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