Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload multiple file using multer failed [closed]

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.

like image 459
Nichole A. Miler Avatar asked Dec 08 '22 00:12

Nichole A. Miler


1 Answers

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

like image 136
Nalla Srinivas Avatar answered Dec 11 '22 07:12

Nalla Srinivas