Upload files using formidable & multiparty

Source: Internet
Author: User

Installation

NPM Install formidable,multiparty–save-d

Example 1: Using formidable to parse the form
Exports.formidableformparse =function(Req,callback) {
var obj ={};
var form =New Formidable. Incomingform ({
Encoding"Utf-8",
Uploaddir:"Public/upload",File Upload Address
Keepextensions:TrueReserved suffix
});
Form.parse (req)
. On (' Field ',function (name, value) {//field
Obj[name] = value;
})
. On ( ' file ', function //file
Obj[name] = file;
})
. On (function//end
callback (Error);
})
. On (function () {//end
callback (null,obj);
});
}
/span>
Note: Multiparty use this method, and can not get the following results, will be an error, do not know is not where I write there is a problem. return value:

{
"Name": "Wuwanyu",
"Age": "23",
"Icon": {
"Size": 8666,
"Path": "Public\upload\upload_713dad980d7b7dce0847476820f8b1d4.jpg",
"Name": "4eff22a5d3d8341d3bf472adbb151c18.jpg",
"Type": "Image/jpeg",
"Mtime": "2016-04-06t13:18:15.508z"
}
}

Example 2: Using multiparty to parse the form
Exports.multipartyformparse =functionReq,callback) {
var form =New multiparty. Form ({
Encoding"Utf-8",
Uploaddir:"Public/upload",File Upload Address
Keepextensions:TrueReserved suffix
})

Form.parse (req,function (err, fields, files) {
var obj ={};
object.keys (Fields). ForEach ( function (name) {
Console.log ( "name: ' + name+ Obj[name] = Fields[name];
});

object.keys (Files). ForEach ( function (name) {
Console.log ( "name: ' + name+"; file: "+files[name]);
Obj[name] = Files[name];
});

Callback (err,obj);
});
}
/span>
Note: Formidable uses the same method of parsing fields, the resulting data is still in JSON format, not in array format. return value (when a file):

{

Span style= "font-family: the song Body;" > "age": ["["],

{


" path ":" Public\upload\skt_xewcxnbd_4qc6qi-pbw9.jpg ",
" headers ": {
Span style= "font-family: the song Body;" > "content-disposition": "Form-data; Name=\ "icon\"; Filename=\ "4eff22a5d3d8341d3bf472adbb151c18.jpg\" ",
" Content-type ":" Image/jpeg "},

" /span>

}

return value (when multiple files):

{
"name": ["Wuwanyu"],
"Age": [""],
"icon": [
{
" fieldName": "Icon",
" originalfilename": "3bd870116ff9708f5141aa8a374aeabf.jpg",
" path": "Public\upload-mvuwggwt9dbgczh50ybglhy.jpg",
"Headers": {
" content-disposition": "Form-data; Name=\ "icon\"; Filename=\ "3bd870116ff9708f5141aa8a374aeabf.jpg\" ",
" content-type": "Image/jpeg"},
"size": 25754
},
{
" fieldName": "Icon",
" originalfilename": "4eff22a5d3d8341d3bf472adbb151c18.jpg",
" path": "Public\upload\hjioayhxjluottwz2m-qwhuq.jpg",
"Headers": {
" content-disposition": "Form-data; Name=\ "icon\"; Filename=\ "4eff22a5d3d8341d3bf472adbb151c18.jpg\" ",
" content-type": "Image/jpeg"},
"size": 8666
}
]
}

Summarize:

Both formidable and multiparty can implement the function of parsing a form, and the data structure and field name of the returned value are different. Formidable parsing results are JSON data format, clear and unambiguous. Multiparty the return value is in array format, it is more appropriate to use multiparty when parsing multiple values in the same field.

The file is uploaded to the temp file directory, we also want to move the temporary files to our upload directory
Fs.rename (File.path, Global.appConfig.uploadDir + '/' + file.filename);

GitHub Code reference address: Https://github.com/wuwanyu/formidable.multipaty.node.test.git

Upload files using formidable & multiparty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.