Multer在通过ng-file上传上传文件时抛出奇怪的错误

Posted

技术标签:

【中文标题】Multer在通过ng-file上传上传文件时抛出奇怪的错误【英文标题】:Multer throwing weird error while uploading file via ng-file upload 【发布时间】:2015-12-30 00:04:05 【问题描述】:

我对此进行了一些研究,但我仍然不知道这里发生了什么。谷歌有不相关的搜索结果或给定的错误代码/消息。

这是我的角度控制器,仅基于 ng-file 上传的 api 代码和 ng-file 上传示例。

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));
            );
        
    ;
]);

这是我在服务器端写的代码。

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

这个我也试过了,想一次性上传,所以想用数组上传功能

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

这是完整的存储代码-服务器

var merchant_storage = multer.diskStorage(
  destination: function (req, file, cb) 
    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);
    );


  ,
  filename: function (req, file, cb) 
  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 );

现在是坏的部分。错误!

当我选择要上传的文件时抛出错误。

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)

我必须补充一点,没有一个 console.log 部分打印在 控制台 - 来自服务器端,来自控制器 前端的打印正确。

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

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" 

【问题讨论】:

【参考方案1】:

答案是multer不接受ng-upload模块中的数组。

@mscdex 在其他线程上给出了答案。

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

真正的答案归功于 mscdex (https://***.com/users/2050455/mscdex)

【讨论】:

以上是关于Multer在通过ng-file上传上传文件时抛出奇怪的错误的主要内容,如果未能解决你的问题,请参考以下文章

如何在angularjs中使用ng-file上传裁剪和上传图像

Firebase Storage 在图像上传时抛出错误(Nodejs)

Undertow 上传多部分文件超过设置值时抛出 RuntimeException

org.eclipse.jetty.io.EofException:上传大文件时抛出早期 EOF

如何使用 multer 或 body-parser 上传文件

RestKit 在 PUT 请求时抛出 NSInternalInconsistencyException