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

Uploading multiple files with multer in express

我试图允许将多个文件上传到我的 Express 应用程序,但我遇到了 error.Whats 此代码错误?

  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('error');
         console.log(err);
     }else{
       res.redirect('/upload?upload success');
        console.log(req.files);
             }
              })

您指定:

upload.single('fileupload')

将其更改为:

upload.array('fileupload')

或者您也可以这样做:

upload.any()

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