Nodejs Express Frame Upload file

Source: Internet
Author: User
Tags form post html form post

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

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.