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

耗尽温柔 提交于 2019-12-12 02:47:09

问题


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

回答1:


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 File Error'

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



来源:https://stackoverflow.com/questions/32895968/multer-throwing-weird-error-while-uploading-file-via-ng-file-upload

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!