Home > Web Front-end > JS Tutorial > body text

Comprehensive analysis of image upload in node form

高洛峰
Release: 2016-12-06 11:08:52
Original
1020 people have browsed it

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
Copy after login

html code

<form action="/api/uppic" method="post" >
<input type="file" name="pic" >
<input type="submit">
</form>
Copy after login

node code

app.route(&#39;/api/uppic&#39;).post(function(req,res){
var multiparty = require(&#39;multiparty&#39;);
var form = new multiparty.Form();//新建表单
//设置编辑
form.encoding = &#39;utf-8&#39;;
//设置图片存储路径
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":&#39;请上传5M以图片&#39;};
res.end(JSON.stringify(u));
return false;
}
//获取路径
var oldpath=files.imgFile[0][&#39;path&#39;];
//文件后缀处理格式
if(oldpath.indexOf(&#39;.jpg&#39;)>=0){
var suffix=&#39;.jpg&#39;;
}else if(oldpath.indexOf(&#39;.png&#39;)>=0){
var suffix=&#39;.png&#39;;
}else if(oldpath.indexOf(&#39;.gif&#39;)>=0){
var suffix=&#39;.gif&#39;;
}else{
var u={"error" :1,"message":&#39;请上传正确格式&#39;};
res.end(JSON.stringify(u));
return false;
}
var url=&#39;Uploads/gaoxiao/&#39;+Date.now()+suffix;
var fs=require(&#39;fs&#39;);
//给图片修改名称
fs.renameSync(oldpath,url);
var u={ "error" : 0, "url" : &#39;/&#39;+url}
res.end(JSON.stringify(u));
});
});
Copy after login

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template