Multer扔奇怪的错误,同时通过NG-文件上传文件上传 [英] Multer throwing weird error while uploading file via ng-file upload

查看:943
本文介绍了Multer扔奇怪的错误,同时通过NG-文件上传文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我做了这个后,一些研究,我仍然不知道是怎么回事。在谷歌有不相关的搜索结果或给定的错误code /消息。

下面是我的角度控制,只是基于API的code和NG-文件上传NG-文件上传的样本。

  VAR应用= angular.module('文件上传',['ngFileUpload']);app.controller('MyCtrl',['$范围','上传','$超时',函数($范围,上传,$超时){
    的console.log(嘿嗬从控制器);
    $ scope.uploadFiles =功能(文件){
        $ scope.files =文件;
        如果(文件和放大器;&安培; files.length){
            的console.log(文件);
            Upload.upload({
                网址:'API /数据/检查',
                数据:{
                    文件:文件
                }
            })。然后(功能(响应){
                $超时(函数(){
                    $ scope.result = response.data;
                    的console.log(响应);
                });
            },函数(响应){
                的console.log(响应);
                如果(response.status大于0){
                    $ scope.errorMsg = response.status +:+ response.data;
                    的console.log(响应);
                }
            },功能(EVT){
                的console.log(响应);
                $ scope.progress =
                    Math.min(100,parseInt函数(100.0 * evt.loaded / evt.total));
            });
        }
    };
}]);

这是code我已经写在服务器端。

  app.post('/ API /数据/检查',merchants_offer_upload.single('文件'),功能(REQ,资源,下一个){
  的console.log(req.body.merchantid);
  的console.log(我们在这里);
   // res.json({状态:0,picarray:array_pic});
});

我也试过这个问题,以及,我想这些在一个上载走,所以我想用数组上传功能

  app.post('/ API /数据/检查',merchants_offer_upload.array('文件'),功能(REQ,资源,下一个){
  的console.log(req.body.merchantid);
  的console.log(我们在这里);
   // res.json({状态:0,picarray:array_pic});
});

这是一个完整的存储code - 服务器

  VAR merchant_storage = multer.diskStorage({
  目的地:功能(REQ,文件,CB){    的console.log(Multer第1部分婊子!)
    MERCHANT_ID = req.params.merchantid;
    的console.log(MERCHANT_ID);
    的console.log('MERCHANT_ID:',MERCHANT_ID);
    mkdirp(__目录名称+'/图片/报价/+ MERCHANT_ID,功能(错误){
      的console.log(__目录名);
      如果(ERR)console.error(ERR)
        CB(NULL,__dirname +'/图片/报价/'+ MERCHANT_ID);
        的console.log(heeeee 1);
    });
  },
  文件名:功能(REQ,文件,CB){
   的console.log(Multer部分2);
  变种F = Date.now()+'_'+ file.originalname;
    CB(NULL,Date.now()+ F)
   array_pic.push(F);
    的console.log(F);
    的console.log(file.originalname);  }
})VAR merchants_offer_upload = multer({存储:merchant_storage});

现在坏的部分。错误!

当我选择文件上传

错误被抛出。

 错误:意外现场
    在makeError(C:\\ nodefiles \\新\\ node_modules \\ multer \\ LIB \\化妆error.js:12:13)
    在wrappedFileFilter(C:\\ nodefiles \\新\\ node_modules \\ multer \\ index.js:39:19)
    在打杂<&匿名GT; (C:\\ nodefiles \\新\\ node_modules \\ multer \\ LIB \\化妆middleware.js:109:7)
    在Busboy.emit(events.js:118:17)
    在Busboy.emit(C:\\ nodefiles \\新\\ node_modules \\ multer \\ node_modules \\打杂\\ lib目录\\ main.js:31:35)
    在PartStream<&匿名GT; (C:\\ nodefiles \\新\\ node_modules \\ multer \\ node_modules \\打杂\\ lib目录\\ \\类型multipart.js:209:13)
    在PartStream.emit(events.js:107:17)
    在HeaderParser<&匿名GT; (C:\\ nodefiles \\新\\ node_modules \\ multer \\ node_modules \\打杂\\ node_modules \\切片机\\ lib目录\\ Dicer.js:51:16)
    在HeaderParser.emit(events.js:107:17)
    在HeaderParser._finish (C:\
odefiles\
ew\
ode_modules\\multer\
ode_modules\\busboy\
ode_modules\\dicer\\lib\\HeaderParser.js:70:8)
    在SBMH<&匿名GT; (C:\
odefiles\
ew\
ode_modules\\multer\
ode_modules\\busboy\
ode_modules\\dicer\\lib\\HeaderParser.js:42:12)
    在SBMH.emit(events.js:118:17)
    在SBMH._sbmh_feed (C:\
odefiles\
ew\
ode_modules\\multer\
ode_modules\\busboy\
ode_modules\\dicer\
ode_modules\\streamsearch\\lib\\sbmh.js:159:14)
    在SBMH.push (C:\
odefiles\
ew\
ode_modules\\multer\
ode_modules\\busboy\
ode_modules\\dicer\
ode_modules\\streamsearch\\lib\\sbmh.js:56:14)
    在HeaderParser.push (C:\
odefiles\
ew\
ode_modules\\multer\
ode_modules\\busboy\
ode_modules\\dicer\\lib\\HeaderParser.js:48:19)
    在Dicer._oninfo(C:\\ nodefiles \\新\\ node_modules \\ multer \\ node_modules \\打杂\\ node_modules \\切片机\\ lib目录\\ Dicer.js:198:25)


  

有一件事我必须补充,而不是单一的console.log一部分被印在
  控制台 - 这是从服务器端,从控制器的那些
  前端的被正确打印。


这是错误消息我收到的控制器端响应

 嘿从控制器浩
controller6.js:12 [文件]
angular.js:10661 POST HTTP://本地主机:1339 / API /数据/检查500(内部服务器错误)(匿名函数)@ angular.js:10661sendReq @ angular.js:10480status $ @ get.serverRequest angular.js :10187processQueue @ angular.js:14634(匿名函数)@ angular.js:14650parent $ get.Scope $ @的eval angular.js:。15916parent $ get.Scope $ @消化angular.js:15727parent $得到。范围$ @适用angular.js:16024(匿名函数)@ angular.js:17791completeOutstandingRequest @ angular.js:5490(匿名函数)@ angular.js:5762
2angular.js:12416的ReferenceError:响应没有定义
    在controller6.js:30
    在angular.js:14716
    在Scope.parent $ get.Scope $的eval(angular.js:15916)。
    。在Scope.parent $ get.Scope $摘要(angular.js:15727)
    。在Scope.parent $ get.Scope $申请(angular.js:16024)
    在完成(angular.js:10511)
    在completeRequest(angular.js:10683)
    在XMLHtt prequest.requestLoaded(angular.js:10624)(匿名函数)@ angular.js:12416ident $ @ GET angular.js:9203(匿名函数)@ angular.js:14718parent $ get.Scope。 $ @的eval angular.js:。15916parent $ get.Scope $ @消化angular.js:15727parent $ get.Scope $ @适用angular.js:。16024done @ angular.js:10511completeRequest @ angular.js:10683requestLoaded @角.js文件:10624
controller6.js:24的对象{数据:错误:意外场< BR>&安培; NBSP;&安培; NBSP;在makeEr内容S \\打杂\\ node_modules \\切片机\\ lib目录\\ Dicer.js:198:25)↵状态:500,配置:对象,状态文本:内部服务器错误}
controller6.js:27的对象{数据:错误:意外场< BR>&安培; NBSP;&安培; NBSP;在makeEr内容S \\打杂\\ node_modules \\切片机\\ lib目录\\ Dicer.js:198:25)↵状态:500,配置:对象,状态文本:内部服务器错误}


解决方案

答案是multer不接受NG-上传模块阵列。

答案被其他线程@mscdex给出。

<一个href=\"http://stackoverflow.com/questions/32917617/multer-not-accepting-files-in-array-format-gives-unexpected-filed-error\">Multer数组形式不接受文件给出了意外提起错误

真正的答案归功于MSCDEX(<一个href=\"http://stackoverflow.com/users/2050455/mscdex\">http://stackoverflow.com/users/2050455/mscdex)

I did some research upon this and I still don't know what is going on here. The google has irrelevant search result or the given error code/message.

Here is my angular controller, simply based upon api code and samples of ng-file upload on ng-file upload.

var app = angular.module('fileUpload', ['ngFileUpload']);

app.controller('MyCtrl', ['$scope', 'Upload', '$timeout', function ($scope, Upload, $timeout) {
    console.log("Hey ho from controller");
    $scope.uploadFiles = function (files) {
        $scope.files = files;
        if (files && files.length) {
            console.log(files);
            Upload.upload({
                url: 'api/data/check',
                data: {
                    files: files
                }
            }).then(function (response) {
                $timeout(function () {
                    $scope.result = response.data;
                    console.log(response);
                });
            }, function (response) {
                console.log(response);
                if (response.status > 0) {
                    $scope.errorMsg = response.status + ': ' + response.data;
                    console.log(response);
                }
            }, function (evt) {
                console.log(response);
                $scope.progress =
                    Math.min(100, parseInt(100.0 * evt.loaded / evt.total));
            });
        }
    };
}]);

This is the code I have written on the server side.

app.post('/api/data/check', merchants_offer_upload.single('files'), function (req, res, next) {
  console.log(req.body.merchantid);
  console.log("we are here");
   // res.json({status: 0, picarray: array_pic});
});

I have tried this as well, I want these to uploaded in one go so I wanted to use array upload function

app.post('/api/data/check', merchants_offer_upload.array('files'), function (req, res, next) {
  console.log(req.body.merchantid);
  console.log("we are here");
   // res.json({status: 0, picarray: array_pic});
});

This is the complete storage code - server

var merchant_storage = multer.diskStorage({
  destination: function (req, file, cb) {

    console.log("Multer Part 1 Bitch!")
    merchant_id = req.params.merchantid;
    console.log(merchant_id);
    console.log('merchant_id : ', merchant_id);
    mkdirp(__dirname + '/images/offers/' + merchant_id, function (err) {
      console.log(__dirname);
      if (err) console.error(err)
        cb(null, __dirname + '/images/offers/' + merchant_id);
        console.log("heeeee 1");
    });


  },
  filename: function (req, file, cb) {
   console.log("Multer Part 2");
  var f =  Date.now()+'_'+file.originalname;
    cb(null,Date.now() + f)
   array_pic.push(f);
    console.log(f);
    console.log(file.originalname);

  } 
})

var merchants_offer_upload = multer({ storage: merchant_storage });

Now the bad part. Errors!

Errors are thrown when I select files to be uploaded.

Error: Unexpected field
    at makeError (C:\nodefiles\new\node_modules\multer\lib\make-error.js:12:13)
    at wrappedFileFilter (C:\nodefiles\new\node_modules\multer\index.js:39:19)
    at Busboy.<anonymous> (C:\nodefiles\new\node_modules\multer\lib\make-middleware.js:109:7)
    at Busboy.emit (events.js:118:17)
    at Busboy.emit (C:\nodefiles\new\node_modules\multer\node_modules\busboy\lib\main.js:31:35)
    at PartStream.<anonymous> (C:\nodefiles\new\node_modules\multer\node_modules\busboy\lib\types\multipart.js:209:13)
    at PartStream.emit (events.js:107:17)
    at HeaderParser.<anonymous> (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\Dicer.js:51:16)
    at HeaderParser.emit (events.js:107:17)
    at HeaderParser._finish (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:70:8)
    at SBMH.<anonymous> (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:42:12)
    at SBMH.emit (events.js:118:17)
    at SBMH._sbmh_feed (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\node_modules\streamsearch\lib\sbmh.js:159:14)
    at SBMH.push (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\node_modules\streamsearch\lib\sbmh.js:56:14)
    at HeaderParser.push (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\HeaderParser.js:48:19)
    at Dicer._oninfo (C:\nodefiles\new\node_modules\multer\node_modules\busboy\node_modules\dicer\lib\Dicer.js:198:25)

One thing I must add, not a single console.log part is printed on the console - which is from the server side, the ones from the controller of the front end is printed properly.

This is error messages I receive as response on the controller side

Hey ho from controller
controller6.js:12 [File]
angular.js:10661 POST http://localhost:1339/api/data/check 500 (Internal Server Error)(anonymous function) @ angular.js:10661sendReq @ angular.js:10480status.$get.serverRequest @ angular.js:10187processQueue @ angular.js:14634(anonymous function) @ angular.js:14650parent.$get.Scope.$eval @ angular.js:15916parent.$get.Scope.$digest @ angular.js:15727parent.$get.Scope.$apply @ angular.js:16024(anonymous function) @ angular.js:17791completeOutstandingRequest @ angular.js:5490(anonymous function) @ angular.js:5762
2angular.js:12416 ReferenceError: response is not defined
    at controller6.js:30
    at angular.js:14716
    at Scope.parent.$get.Scope.$eval (angular.js:15916)
    at Scope.parent.$get.Scope.$digest (angular.js:15727)
    at Scope.parent.$get.Scope.$apply (angular.js:16024)
    at done (angular.js:10511)
    at completeRequest (angular.js:10683)
    at XMLHttpRequest.requestLoaded (angular.js:10624)(anonymous function) @ angular.js:12416ident.$get @ angular.js:9203(anonymous function) @ angular.js:14718parent.$get.Scope.$eval @ angular.js:15916parent.$get.Scope.$digest @ angular.js:15727parent.$get.Scope.$apply @ angular.js:16024done @ angular.js:10511completeRequest @ angular.js:10683requestLoaded @ angular.js:10624
controller6.js:24 Object {data: "Error: Unexpected field<br> &nbsp; &nbsp;at makeEr…s\busboy\node_modules\dicer\lib\Dicer.js:198:25)↵", status: 500, config: Object, statusText: "Internal Server Error"}
controller6.js:27 Object {data: "Error: Unexpected field<br> &nbsp; &nbsp;at makeEr…s\busboy\node_modules\dicer\lib\Dicer.js:198:25)↵", status: 500, config: Object, statusText: "Internal Server Error"} 

解决方案

The answer is that multer do not accept array in ng-upload module.

answer was given by @mscdex on other thread.

Multer not accepting files in array format gives 'Unexpected Filed Error'

The real answer credit goes to mscdex (http://stackoverflow.com/users/2050455/mscdex)

这篇关于Multer扔奇怪的错误,同时通过NG-文件上传文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆