express3.0 the pits encountered when upgrading 4.0

Source: Internet
Author: User

Today to do the project migration encountered a pit, about file upload, after express4.0+, Bodyparser do not deal with the problem of files.

Before the project using the express3.0+ version, in doing the upload of Excel through Node-xlsx file resolution, the upgrade after the express4.0 not to parse the upload file, that is, in your body can not find the upload of the asked pieces. New modules need to be added to meet previous requirements. We're going to introduce ' connect-multiparty ' to deal with uploading files.

4.0 after uploading the file, your body inside is not the parameters, and can not find files this parameter.

We need to introduce in the code

var multipart = require (' Connect-multiparty ');
var multipartmiddleware = multipart ();

Print your req parameters in the spool, as visible:

By the way, the code that node parses Excel for reference to learn.

Node used in the background, the following is the service-side code

var multipart = require (' Connect-multiparty ');
var multipartmiddleware = multipart ();
var xlsx = require (' node-xlsx ');
App.all ('/yourpath ', Multipartmiddleware,function(req, res) {vardata, error, Fpath, ResJson, result, Sheet1, sheets; ResJson=function(JSON) {Res.setheader (' Content-type ', ' text/html; Charset=utf-8 '); returnres.end (Json.stringify (JSON));        }; Result={bstatus: {code:0, des:' Success '}, Data: {errors: []}}; Fpath=Req.files.file.path; Try{Sheets=Xlsx.parse (Fpath); if(sheets.length) {Sheet1= Sheets[0]; } Data= []; if(sheet1.data) {data=Sheet1.data; }        } Catch(_error) {error=_error; Result.bstatus.code= 902; Result.bstatus.des= ' uploaded file is not Excel, or there are data exceptions that the system cannot handle '; returnResJson (Result); }    });

Front end using ajaxfileupload components for uploading

 varOptions ={type:' Post ', URL:' Yourpath ',//server-side request address for file uploadsSecureuri:false,//whether a security protocol is required, generally set to falseFileelementid:fileelementid,//ID of the file upload domainDataType: ' JSON ',//The return value type is generally set to JSONData:data, Success:function(data, status) {//Server Success Response handler function                        //Successful callback}, Error:function(data, status, E) {//Server Response Failure handler function                        //Failure Callback                    }                };        $.ajaxfileupload (options); 

express3.0 the pits encountered when upgrading 4.0

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.