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

Multer throwing weird error while uploading file via ng-file upload

我对此做了一些研究,但我仍然不知道这里发生了什么。 google 具有不相关的搜索结果或给定的错误 code/message。

这是我的 angular 控制器,仅基于 api 代码和 ng 文件上传上的 ng 文件上传示例。

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

现在是糟糕的部分。错误!

当我要上传 select 个文件时抛出错误。

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.

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

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"} 

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

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

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