A node.js module for dealing with web forms.
- Fast (~500mb/sec), non-buffering multipart parser
- Automatically writing file uploads to disk
- Low memory footprint
- Graceful error handling
- Very high test coverage
- Implement QuerystringParser the same way as MultipartParser
Via npm:
npm install formidable@latest
Manually:
git clone git://github.com/felixge/node-formidable.git formidable
vim my.js
# var formidable = require('./formidable');
Note: Formidable requires gently to run the unit tests, but you won't need it for just using the library.
Parse an incoming file upload.
var formidable = require('formidable')
, http = require('http')
, sys = require('sys');
http.createServer(function(req, res) {
if (req.url == '/upload' && req.method.toLowerCase() == 'post') {
// parse a file upload
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(sys.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>'
);
});
Creates a new incoming form.
The encoding to use for incoming form fields.
The directory for placing file uploads in. You can later on move them using fs.rename()
.
If you want the files written to incomingForm.uploadDir
to include the extensions of the original files, set this property to true
.
Either 'multipart' or 'urlencoded' depending on the incoming request.
Limits the amount of memory a field (not file) can allocate in bytes.
I this value is exceeded, an 'error'
event is emitted. The default
size is 2MB.
The amount of bytes received for this form so far.
The expected number of bytes in this form.
Parses an incoming node.js request
containing form data. If cb
is provided, all fields an files are collected and passed to the callback:
incomingForm.parse(req, function(err, fields, files) {
// ...
});
You may overwrite this method if you are interested in directly accessing the multipart stream. Doing so will disable any 'field'
/ 'file'
events processing which would occur otherwise, making you fully responsible for handling the processing.
incomingForm.onPart = function(part) {
part.addListener('data', function() {
// ...
});
}
If you want to use formidable to only handle certain parts for you, you can do so:
incomingForm.onPart = function(part) {
if (!part.filename) {
// let formidable handle all non-file parts
incomingForm.handlePart(part);
}
}
Check the code in this method for further inspiration.
Emitted after each incoming chunk of data that has been parsed. Can be used to roll your own progress bar.
Emitted whenever a field / value pair has been received.
Emitted whenever a field / file pair has been received. file
is a JS object with the following properties:
{ path: 'the path in the uploadDir this file was written to'
, filename: 'the name this file had on the computer of the uploader'
, mime: 'the mime type specified by the user agent of the uploader'
}
Emitted when there is an error processing the incoming form. A request that experiences an error is automatically paused, you will have to manually call request.resume()
if you want the request to continue firing 'data'
events.
Emitted when the entire request has been received, and all contained files have finished flushing to disk. This is a great place for you to send your response.
Formidable is licensed under the MIT license.
- multipart-parser: a C++ parser based on formidable
- Ryan Dahl for his work on http-parser which heavily inspired multipart_parser.js