Full parsing node form picture upload _node.js

Source: Internet
Author: User
Tags error handling http request

node to fully parse the form of the picture upload, multiparty resolution and content type of HTTP request Multipart/form-data, also known as file upload.

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 ()//new Form//set Edit form.encoding = ' utf-8 ';
Set the picture storage path Form.uploaddir = "uploads/gaoxiao/"; Form.keepextensions = true; Reserved suffix form.maxfieldssize = 2*1024*1024;
Memory size form.maxfilessize= 5*1024*1024;//file byte size limit, exceeding the error err/form parsing form.parse (req, function (err,fields,files) {//Error handling
if (err) {Console.log (err); var u={"error": 1, "message": ' Please upload 5M with picture '}; Res.end (Json.stringify (U)), return false;}//Get Path
var oldpath=files.imgfile[0][' path ']; 
File suffix processing format 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": ' 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 name of the picture Fs.renamesync (Oldpath,url); var u={"error": 0, "url": '/' +url} res.end (json.stringify(u));
}); });

Multiparty

multiparty. form to create a new format * *

Encoding: For Input form field set encoding. Default is UTF8

Maxfieldssize: The amount of all fields (rather than files) that limit memory can be allocated in bytes. If this value is exceeded, an error event is fired. The default size is 2MB.

Maxfields: Limit, the event that will emit an error before parsing the number of fields. A file counts as a field in this case. The default is 1000.

Maxfilessize: Upload file size limit only if relevant autofiles true. Limit the total number of bytes that are accepted for merging all files. If this value is exceeded, an error event is fired. 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 the relevant autofiles is true. Directory placement File upload. You can use to move them later fs.rename (). The default is Os.tmpdir ().

Form.parse (Req,function (err,fields,files) {})

-Fields: Is an object (the upload name and value) whose Property name field name and value are an array of field values.

-Files: is an object (upload name and server file path) whose property name field name and value is an array of file objects.

File in Files-objects with these properties:

-fieldname-with name-the name of the field in this file

-OriginalFilename-file name, reports for users of this file

-Path-absolute path to upload files on disk

-Headers-This is the HTTP header sent with the file

-Size-sizes of files in bytes

Node form resolution (multiparty) API address: Https://www.npmjs.com/package/multiparty

The above is a small set to introduce a comprehensive resolution node form of the picture upload, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

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.