nodejs express 框架 上傳文件

web 項目應用express4.0框架html

html 表單post 文件上傳失敗,後端沒法獲取提交文件前端

express不支持文件上傳。node

方式一angularjs

如果圖片,能夠將圖片轉碼爲BASE64上傳web

  前端框架angularjs代碼 轉換代碼以下express

 $scope.filechange=function(index){
        var file = this.files[0];
        var url = webkitURL.createObjectURL(file);
        /* 生成圖片
         * ---------------------- */
        var $img = new Image();
        $img.src = url;
        $img.onload = function () {
            //生成比例
            var width = this.width,
                height = this.height,
                scale = width / height;
            width = parseInt(800);
            height = parseInt(width / scale);
            //生成canvas
            var $canvas = $('#canvas');
            var ctx = $canvas[0].getContext('2d');
            $canvas.attr({ width: width, height: height });
            ctx.drawImage($img, 0, 0, width, height);
            var base64 = $canvas[0].toDataURL('image/jpeg', 0.5);
            var cc = $("#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");
        });
    }

 

方式二canvas

nodejs 後端引用connect-multiparty包後端

後端代碼以下前端框架

路由session

 var multipart = require('connect-multiparty');
    var multipartMiddleware = multipart();
    app.post('/q/people/insertPeoples', multipartMiddleware, 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})
        }

    });

業務邏輯實現

function insertPeoples(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) {
                });
                var allpeoples=[];
                var keys=['_id','idcard','wechatuid','qqnum','mac','name'];
                console.log(JSON.stringify(output));
                output.forEach(function(people){
                    var peopleobj={};
                    for(var i =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');
                }
            });
        });
    })
}
相關文章
相關標籤/搜索