question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

unable to send multiple files using node, getting Error: unexpected field

See original GitHub issue

I am using Angular ng-file-upload to upload files. When sending here is the html, but I am getting error ‘Error: Unexpected field’ and from the client getting Internal Error 500. Can you please assist with this issue?

<div class="button btn btn-danger" ngf-select ng-model="files" name="files" ngf-multiple="true">Select</div>
  <button type="submit" ng-click="submit()">submit</button>

Angular as follows

if($scope.files){
        console.log('upload multiple works');
        console.log($scope.files);
        $scope.uploadFiles($scope.files);
      }
    };

$scope.uploadFiles = function (files) {
      if (files && files.length) {
        for (var i = 0; i < files.length; i++) {
          Upload.upload({
            url: 'api/admin/photos',
            data: {file: files}
          }).then(function(resp){
            console.log('Successfully ' + resp.config.data.file.name);
            $scope.myPic = resp.config.data.file.name;
          },
          function(resp){
            console.log('Error status: ' + resp.status);
          },
          function(evt){
            var progressPercentage = parseInt(100.0 * evt.loaded / evt.total);
             console.log('progress: ' + progressPercentage + '% ' + evt.config.data.file.name);
          })
          }
        }

      };

and using node

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, 'public/img')
  },
  filename: function (req, file, cb) {
    //cb(null, file.originalname + '-' + Date.now())
    cb(null, file.originalname )
  }
});

var upload = multer({ storage: storage });

//upload photos
apiRoutes.post('/admin/photos',upload.array('files'),function(req,res){

    console.log(req);


    //save to database



    console.log(req);
     console.log(req.file.originalname);
    console.log(req.file.originalname);
     res.json(req.file[0]);

});

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:17 (4 by maintainers)

github_iconTop GitHub Comments

35reactions
shivam1401-zzcommented, May 8, 2016

Change your code. from :

Upload.upload({
            url: 'api/admin/photos',
            data: {file: files}
 })

to:

Upload.upload({
            url: 'api/admin/photos',
            arrayKey: '',
            data: {file: files}
          })

It should work.

6reactions
josdujcommented, Jun 11, 2018

Just use any() instead of array('file').

When using array('file'), multer throws error because it expects all fields to be named file, while ng-file-upload names them file[0], file[1], etc… I wouldn’t recommend using arrayKey: '' because it’s basically a hack, and messes up serialization of other arrays if you are sending additional data.

Another option is to use files(), and do something like:

upload.files([
    { name: 'file[0]', maxCount: 1 },
    { name: 'file[1]', maxCount: 1 },
    ...
])

But then req.files is object with arrays of files.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fix "Unexpected field" Error From Multer - Maxim Orlov
When you see a MulterError: Unexpected field error, you don't suspect the reason to be the number of files exceeding the configured limit....
Read more >
getting Error: Unexpected field when trying to upload multiple ...
If you upload multiple files and use upload.array('files') then your uploaded files are available in req.files , and this is an array of ......
Read more >
Fix "Unexpected field" Error From Multer - YouTube
Learn how to decypher & fix this cryptic error message from multer, and finally implement working file uploads in Node.js LinksRequest ...
Read more >
Node.js Express File Upload Rest API example using Multer
In this tutorial, I will show you how to upload file with Node.js Express Rest APIs to/from a static folder using Multer (with...
Read more >
File upload | NestJS - A progressive Node.js framework
Warning Multer cannot process data which is not in the supported multipart format ( multipart/form-data ). Also, note that this package is not...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found