【问题标题】:Uploading multiple files with multer in express使用 multer 快速上传多个文件
【发布时间】:2021-05-02 05:52:44
【问题描述】:

我试图允许将多个文件上传到我的 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('error');
         console.log(err);
     }else{
       res.redirect('/upload?upload success');
        console.log(req.files);
             }
              })

【问题讨论】:

    标签: node.js express multer


    【解决方案1】:

    您指定:

    upload.single('fileupload')
    

    将其更改为:

    upload.array('fileupload')
    

    或者你也可以这样做:

    upload.any()
    

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-09-06
      • 2017-01-13
      • 2019-01-26
      • 2020-05-31
      • 1970-01-01
      • 1970-01-01
      • 2017-11-12
      • 1970-01-01
      相关资源
      最近更新 更多