Today summarizes the use of the formidable module under node. js and gives some brief instructions below.
1) Create Formidable.incomingform Object
var form = new Formidable. Incomingform ()
2) form.encoding = ' Utf-8 ' Sets the encoding of the form field
3) Form.uploaddir = "/my/dir"; Set the folder to upload files, default to the system's temporary folder, you can use Fs.rename () to change the location and file name of the uploaded files
4) Form.keepextensions = false; Setting this property to True allows the uploaded file to keep the original file extension.
5) Form.type read-only, depending on the type of request, take the value ' multipart ' or ' urlencoded '
6) Form.maxfieldssize = 2 * 1024 * 1024; Limits the size of all stored form fields (except the File field) and, if exceeded, triggers an error event, which defaults to 2M
7) Form.maxfields = 1000 Set How many query strings can be converted, default is 1000
8) Form.hash = false; Set the verification code of the uploaded file, can have two value ' SHA1 ' or ' MD5 '.
9) Form.multiples = false; Turn on this feature, when the Form.parse () method is called, the files parameter of the callback function will be a file array, each member of the array is a file object, and this function needs to be supported by the multiple feature in HTML5.
form.bytesreceived returns how many bytes the server has received the current form data
One) form.bytesexpected returns the size of all data that will be received for the current form
Form.parse (Request, [callback]) The method transforms the form data contained in the request, Callback contains all field fields and file information, such as:
Form.parse (req, Function (Err, fields, files) {
// ...
});
Form.onpart (part); You can overload the method of handling the multipart stream, which prevents field and file events from occurring, and you will have to handle these things yourself, such as:
Form.onpart = function (part) {
Part.addlistener (' Data ', function () {
// ...
});
}
If you just want formdable to deal with some things, you can do this:
Form.onpart = function (part) {
if (!part.filename) {
Let formidable process all non-file parts
Form.handlepart (part);
}
}
) Formidable. File Object
A. file.size = 0 The size of the uploaded file, if the file is being uploaded, indicates the size of the uploaded part
B. File.path = NULL The path of the uploaded file. If you do not want formidable to produce a temporary folder, you can modify the path in the Filebegain event
C. File.name = NULL The name of the uploaded file
D. File.type = null The MIME type of the uploaded file
E. file.lastmodifieddate = null time object, the time when the file was last modified
F. File.hash = NULL Returns the hash value of the file
G. can use Json.stringify (File.tojson ()) to format the output file information
Form.on (' Progress ', function (bytesreceived, bytesexpected) {}); This event is triggered when a block of data is processed and is useful for creating a progress bar.
Form.on (' field ', function (name, value) {}); This event is triggered whenever a field/value pair has been received
Form.on (' Filebegin ', function (name, file) {}); This event is triggered when any new file is detected in the post stream
Form.on (' file ', function (name, file) {}); This event is triggered whenever a pair of fields/files have been received
Form.on (' Error ', function (err) {}); This event is triggered when an error occurs in the upload stream, and you must manually call the Request.resume () method if you want to continue triggering the data event of request when an error occurs.
Form.on (' aborted ', function () {}); The event is triggered when the user aborts the request, and the timeout and close events in the socket also trigger the event, and the error event fires when the event is triggered
) Form.on (' End ', function () {}); This event is triggered when all requests have been received and all of the files have been uploaded to the server. The request can now be sent to the client.
Use of the formidable module for node. js