Web Project Application express4.0 Framework
HTML form Post File upload failed, backend cannot get submit file
Express does not support file uploads.
Way One
If the picture, you can transcode the image to BASE64 upload
The front-end framework Angularjs code Conversion code is as follows
$scope. filechange=function(index) {varFile = This. files[0]; varURL =webkiturl.createobjecturl (file); /*Create picture *----------------------*/ var$img =NewImage (); $img. SRC=URL; $img. OnLoad=function () { //Build Scale varwidth = This. width, height= This. Height, scale= width/height; Width= parseint (800); Height= parseint (Width/Scale ); //Build Canvas var$canvas = $ (' #canvas '); varCTX = $canvas [0].getcontext (' 2d ')); $canvas. attr ({width:width, height:height}); Ctx.drawimage ($img,0, 0, width, height); varBase64 = $canvas [0].todataurl (' Image/jpeg ', 0.5); varCC = $ ("#pic" +index); Cc.attr ("SRC", URL); postdata["File" + index] = base64.substr (23); }} $scope. Upload=function() {$http. Post (' Uploadfile/fenxiangimg?wxopenid=222&orderid=111&templateid=1 ', $scope. postdata). Success (function(data) {alert ("Sucess"); }); }
Way Two
Nodejs back-end reference Connect-multiparty package
The back-end code is as follows
Routing
var multipart = require (' Connect-multiparty '); var multipartmiddleware = multipart (); App.post (function(req, res) { if(req.session&& Req.session.account) { var filepath=Req.files.peoples.path; Console.log (req); People.insertpeoples (Req,res,filepath); } Else { res.send ({error:1}) } );
Business logic Implementation
functionInsertpeoples (Req,res,filepath) {basemodule.checkaccountrole (req,res,function(account) {Fs.readfile (filepath,function(Err, filedata) {Console.log (Err+filedata); Csv.parse (Filedata, {comment:‘#‘},function(err, Output) {Fs.unlink (filepath,function(Err) {}); varallpeoples=[]; varkeys=[' _id ', ' idcard ', ' wechatuid ', ' qqnum ', ' mac ', ' name ']; Console.log (json.stringify (output)); Output.foreach (function(people) {varpeopleobj={}; for(varI =0;i<keys.length;i++) {Peopleobj[keys[i]]=People[i]; } if(Peopleobj.mac) {Peopleobj.mac=peopleobj.mac.toUpperCase (); } allpeoples.push (Peopleobj); }); if(allpeoples.length>0) {db.insertpeoples (Allpeoples,account,function(err,doc) {if(!err) {Res.redirect ('/#people '); }Else{Res.send ({error:1})} }) } Else{res.redirect ('/#people '); } }); }); })}
Nodejs Express Frame Upload file