Home > Article > Web Front-end > Comprehensive analysis of image upload in node form
node comprehensively parses form image uploads, multiparty parses HTTP requests with content type multipart/form-data, also known as file uploads.
multiparty installation
npm install multiparty
html code
<form action="/api/uppic" method="post" > <input type="file" name="pic" > <input type="submit"> </form>
node code
app.route('/api/uppic').post(function(req,res){ var multiparty = require('multiparty'); var form = new multiparty.Form();//新建表单 //设置编辑 form.encoding = 'utf-8'; //设置图片存储路径 form.uploadDir = "Uploads/gaoxiao/"; form.keepExtensions = true; //保留后缀 form.maxFieldsSize = 2*1024*1024; //内存大小 form.maxFilesSize= 5*1024*1024;//文件字节大小限制,超出会报错err //表单解析 form.parse(req, function(err,fields,files) { //报错处理 if(err){ console.log(err); var u={"error" :1,"message":'请上传5M以图片'}; res.end(JSON.stringify(u)); return false; } //获取路径 var oldpath=files.imgFile[0]['path']; //文件后缀处理格式 if(oldpath.indexOf('.jpg')>=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":'请上传正确格式'}; res.end(JSON.stringify(u)); return false; } var url='Uploads/gaoxiao/'+Date.now()+suffix; var fs=require('fs'); //给图片修改名称 fs.renameSync(oldpath,url); var u={ "error" : 0, "url" : '/'+url} res.end(JSON.stringify(u)); }); });
multiparty
multiparty.Form Create a new form**
encoding: for input form Field set encoding. Defaults to utf8
maxFieldsSize: Limits the amount of memory that all fields (not files) can allocate in bytes. If this value is exceeded, an error event is emitted. The default size is 2MB.
maxFields: Limits the number of fields that will be parsed before an error event is emitted. A file counts as a field in this case. The default is 1000.
maxFilesSize: Upload file size limit, only when related autoFiles is true. Limit the total number of bytes accepted for all files merged. If this value is exceeded, an error event is emitted. 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 if related autoFiles is true. Directory to place files for upload. You can move them later using fs.rename(). The default is os.tmpDir().
form.parse(req, function(err,fields,files){})
- fields: is an object (upload name and value), the field name and value of its attribute name are arrays of field values.
- files: is an object (upload name and server file path), the field name and value of its attribute name are arrays of file objects.
file object file - an object with these properties:
- fieldName - the same as name - the field name in this file
- originalFilename - the file name, for the user's report of this file
- path - Absolute path to upload file on disk
- headers - These are the HTTP headers sent with the file
- size - File size in bytes
Node form parsing (multiparty) api address: https:/ /www.npmjs.com/package/multiparty