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

时间:2015-10-01 20:06:46

标签: javascript angularjs express file-upload multer

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

这是我的角度控制器,只需基于api代码和ng-file上传的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);
  console.log("we are here");
   // 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);
  console.log("we are here");
   // res.json({status: 0, picarray: array_pic});
});

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

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

现在不好的部分。错误!

选择要上载的文件时会引发错误。

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部分打印在   控制台 - 来自服务器端,来自控制器的控制台   前端打印正确。

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

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

1 个答案:

答案 0 :(得分:0)

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

@mscdex在其他帖子上给出了答案。

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

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

相关问题