Renaming an uploaded file using Multer doesn't work (Express.js)

后端 未结 6 1710
孤独总比滥情好
孤独总比滥情好 2021-02-07 20:43

I\'m trying to upload a file from a HTML form using Express.js and Multer. I\'ve managed to save the file to the desired location (a folder named uploads).

Howe

6条回答
  •  一个人的身影
    2021-02-07 21:43

    try this way which i'm using

      var storage = multer.diskStorage({
        destination: function (req, file, cb) {
          cb(null, 'uploads/')
        },
        filename: function (req, file, cb) {
          console.log(file);
          var fileObj = {
            "image/png": ".png",
            "image/jpeg": ".jpeg",
            "image/jpg": ".jpg"
          };
          if (fileObj[file.mimetype] == undefined) {
            cb(new Error("file format not valid"));
          } else {
            cb(null, file.fieldname + '-' + Date.now() + fileObj[file.mimetype])
          }
        }
      })
    
      var upload = multer({ storage: storage })
    

提交回复
热议问题