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

0
154

we give a random name to file with the help of date and appends the original file extension with help of file.mimetype

try console.log(file.mimetype) you will get the file name and extension separated by ‘/’ then I split it to array and fetch the extension from it. Try the below code.

let storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, './uploads')
  },
  filename: function (req, file, cb) {
    let extArray = file.mimetype.split("/");
    let extension = extArray[extArray.length - 1];
    cb(null, file.fieldname + '-' + Date.now()+ '.' +extension)
  }
})
const upload = multer({ storage: storage })

Reference Link :- https://stackoverflow.com/questions/32184589/renaming-an-uploaded-file-using-multer-doesnt-work-express-js

LEAVE A REPLY

Please enter your comment!
Please enter your name here