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

后端 未结 6 1702
孤独总比滥情好
孤独总比滥情好 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:29

    The usage for Multer has changed.

    Currently Multer constructor accepts only three options:

    1. dist/storage
    2. fileFilter
    3. limits

    now rename, onFileUploadStart, onFileUploadComplete would not work.

    however renaming can be done using DiskStorage

    var storage = multer.diskStorage({
        destination: function (req, file, cb) {
            cb(null, '/tmp/my-uploads')
        },
        filename: function (req, file, cb) {
            cb(null, file.fieldname + '-' + Date.now())
      }
    })
    
    var upload = multer({ storage: storage })
    

    have a look at these links:

    • https://github.com/expressjs/multer
    • multer callbacks not working ?

提交回复
热议问题