| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
parent directory.. | ||||
Parse http requests with content-type multipart/form-data, also known as file uploads.
See also busboy - a faster alternative which may be worth looking into.
npm install multiparty
Parse an incoming multipart/form-data request.
var multiparty = require('multiparty')
, http = require('http')
, util = require('util')
http.createServer(function(req, res) {
if (req.url === '/upload' && req.method === 'POST') {
// parse a file upload
var form = new multiparty.Form();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
return;
}
// show a file upload form
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(8080);var form = new multiparty.Form(options)Creates a new form. Options:
Parses an incoming node.js request containing form data. If cb is provided, autoFields and autoFiles are set to true and all fields and files are collected and passed to the callback:
form.parse(req, function(err, fields, files) {
// ...
});fields is an object where the property names are field names and the values are arrays of field values.
files is an object where the property names are field names and the values are arrays of file objects.
The amount of bytes received for this form so far.
The expected number of bytes in this form.
Unless you supply a callback to form.parse, you definitely want to handle this event. Otherwise your server will crash when users submit bogus multipart requests!
Only one 'error' event can ever be emitted, and if an 'error' event is emitted, then 'close' will not be emitted.
Emitted when a part is encountered in the request. part is a ReadableStream. It also has the following properties:
Emitted when the request is aborted. This event will be followed shortly by an error event. In practice you do not need to handle this event.
Emitted after all parts have been parsed and emitted. Not emitted if an error event is emitted. This is typically when you would send your response.
By default multiparty will not touch your hard drive. But if you add this listener, multiparty automatically sets form.autoFiles to true and will stream uploads to disk for you.
The max bytes accepted per request can be specified with maxFilesSize.
If you set the form.hash option, then file will also contain a hash property which is the checksum of the file.
| Back | FazBrowse Home | New Git URL |