Complete parsing of node form image uploading node full parsing of form image uploading, multiparty parsing and Content-Type HTTP request multipart/form-data, also known as file upload.
Multiparty Installation
npm install multiparty
Html code
Node Code
App. route ('/api/uppic '). post (function (req, res) {var multiparty = require ('multipart'); var form = new multiparty. form (); // create a form // set and edit Form. encoding = 'utf-8'; // sets the image storage path form. uploadDir = "Uploads/gaoxiao/"; form. keepExtensions = true; // retain the suffix form. maxFieldsSize = 2*1024*1024; // memory size form. maxFilesSize = 5*1024*1024; // the file size limit. If the file size exceeds the limit, the error err // form parsing form is returned. parse (req, function (err, fields, files) {// error handling if (err) {console. log (err); var u = {"error": 1, "message": 'upload 5 MB to image '}; res. end (JSON. stringify (u); return false;} // obtain the path var oldpath = files. imgFile [0] ['path']; // optional ')> = 0) {var suffix='.jpg';} else if(oldpath.indexOf('.png ')> = 0) {var suffix='.png ';} else if(oldpath.indexOf('.gif ')> = 0) {var suffix='.gif';} else {var u = {"error": 1, "message": 'Please upload the correct format '}; res. end (JSON. stringify (u); return false;} var url = 'uploads/gaoxiao/'+ Date. now () + suffix; var fs = require ('fs'); // modify the image name fs. renameSync (oldpath, url); var u = {"error": 0, "url": '/' + url} res. end (JSON. stringify (u ));});});
Multiparty
Create a new Form for multiparty. Form **
Encoding: used to encode the input form field set. The default value is utf8.
MaxFieldsSize: all fields (not files) in the memory can be allocated in bytes. If this value is exceeded, an error event is triggered. The default size is 2 MB.
MaxFields: restriction. An error event is triggered for the number of parsed fields. A file is counted as a field in this case. The default value is 1000.
MaxFilesSize: Specifies the size of the file to be uploaded. this parameter is only valid when autoFiles are set to true. Limit the total number of bytes allowed to merge all files. If this value is exceeded, an error event is triggered. The default value is infinity.
AutoFields: fields that enable field events and disable part events. This is automatically set to true if a field listener is added.
UploadDir: only true for related autoFiles. Directory placement file upload. You can move them fs. rename () later (). The default value is OS. tmpDir ().
Form. parse (req, function (err, fields, files ){})
-Fields: an object (upload name and value). The field name and value of its attribute name are an array of field values.
-Files: an object (upload name and Server File Path). The field name and value of its attribute name are an array of file objects.
Files file-objects with these attributes:
-FieldName-same name-field name in this file
-OriginalFilename-File Name: user report for this file
-Path-absolute path for uploading files to the disk
-Headers-this is the HTTP header sent together with the file
-Size-file size in bytes
Api address for node form parsing (multiparty): https://www.npmjs.com/package/multiparty