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





![SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key length is 767 bytes](https://blog.rahulbhutani.com/wp-content/uploads/2020/04/123-100x70.jpg)










