Upload multiple file using multer failed

Nichole A. Miler picture Nichole A. Miler · Jan 5, 2016 · Viewed 21.2k times · Source

I got empty array doing below function after follow the documentation.

var multer  = require('multer');
var upload = multer();
    router.post('/image', upload.array('photos', 4), function(req, res) {
      var file = req.files;
      console.log('======')
      console.log(file);
      res.end();
    });

<form action="/products/image" method="post" enctype="multipart/form-data">
  <input type="file" name="file" value="upload" multiple>
  <input type="submit" value="upload">
</form>

But the strange thing is this actually work

router.post('/image',upload.single('avatar'), function(req, res) {
  var file = req.files;
  console.log('======')
  console.log(file);
  res.end();
});

with single file upload.

Answer

Nalla Srinivas picture Nalla Srinivas · Jan 5, 2016

Upload.array() required field name and maxCount params. But you have given name of the file is 'file' and you are using middleware upload.array('photos',4). Try in below way it may work

  upload.array('file',4)

or

  upload.any();

please refer the doc: https://github.com/expressjs/multer