I wanted to change my filename but when I examine console.log (req.file
) I can see the filename I changed to, but a different filename is saved in the database using Multergridfs Storage
.
1. the default filename
const storage = new GridFsStorage({
url: config.db,
file: (req, file) => {
return new Promise((resolve, reject) => {
crypto.randomBytes(16, (err, buf) => {
if (err) {
return reject(err)
}
const filename = 'file' + path.extname(file.originalname);
const fileInfo = {
filename: filename,
bucketName: 'contents'
};
resolve(fileInfo);
});
});
}});
2 this is where i edited the filename
router.post('/', upload.single('file'), (req, res) => {
req.file.filename = req.body.fileName + path.extname(req.file.originalname)
res.redirect('/upload/files')
console.log(req.file)
});
the result of the console is something like
{ fieldname: 'file', originalname: '\'YOU CAN ALSO BE GREAT\' - Elon Musk Motivation - Motivational Video.mp4', encoding: '7bit', mimetype: 'video/mp4', id: 5bfb292c13eec142f6c20fd9, filename: 'a.mp4', metadata: null, bucketName: 'contents', chunkSize: 261120, size: 19372377, md5: '513c6220ef3afff644cf8a6dc4cd9130', uploadDate: 2018-11-25T22:58:52.625Z, contentType: 'video/mp4' } { fileName: 'a' }