在express中使用multer上传多个文件

wek*_*sar 0 javascript node.js express multer

我试图允许将多个文件上传到我的 Express 应用程序,但我遇到了错误。这段代码有什么问题?

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);
  }
});

Run Code Online (Sandbox Code Playgroud)

Nen*_*vic 6

您指定:

upload.single('fileupload')
Run Code Online (Sandbox Code Playgroud)

将其更改为:

upload.array('fileupload')
Run Code Online (Sandbox Code Playgroud)

或者你也可以这样做:

upload.any()
Run Code Online (Sandbox Code Playgroud)

如果选择upload.any(),则可以上传一个或多个文件,并且不需要指定字段名称。