Upload multiple file using multer failed [closed]

2019-02-07 12:56发布

问题:

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.

回答1:

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