最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Uploading multiple files with multer in express - Stack Overflow

programmeradmin3浏览0评论

I was trying to allow uploading of multiple files to my express app but I fell into an error. What's wrong with this code?

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./uploaded");
  },

  filename: function (req, file, cb) {
    cb(null, file.originalname);
  },
});

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

router.post("/upload_img", upload.single("fileupload"), (req, res, err) => {
  if (err) {
    console.log(err);
  } else {
    res.redirect("/upload?upload success");
    console.log(req.files);
  }
});

I was trying to allow uploading of multiple files to my express app but I fell into an error. What's wrong with this code?

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "./uploaded");
  },

  filename: function (req, file, cb) {
    cb(null, file.originalname);
  },
});

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

router.post("/upload_img", upload.single("fileupload"), (req, res, err) => {
  if (err) {
    console.log(err);
  } else {
    res.redirect("/upload?upload success");
    console.log(req.files);
  }
});

Share Improve this question edited Nov 21, 2023 at 12:34 wekesar asked May 1, 2021 at 21:43 wekesarwekesar 212 silver badges9 bronze badges 0
Add a ment  | 

1 Answer 1

Reset to default 7

You specified:

upload.single('fileupload')

Change that to:

upload.array('fileupload')

Or you can also do this:

upload.any()

If you go with upload.any(), you can upload one file or multiple files, and you don't need to specify field name.

发布评论

评论列表(0)

  1. 暂无评论