我想更改我的文件名,但是当我检查 console.log( req.file
) 时,我可以看到我更改为的文件名,但是使用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)
});
控制台的结果类似于
{ fieldname: 'file', originalname: '\'YOU ALSO BE GREAT\' - Elon Musk Motivation - Motivational Video.mp4', encoding: '7bit', mimetype: 'video/mp4', id: 5bfb292c13eec142f6c20fd9, filename: 'a.mp4',元数据:null,bucketName:'contents',chunkSize:261120,大小:19372377,md5:'513c6220ef3afff644cf8a6dc4cd9130',uploadDate:2018-11-25T22:58:52.625Z,contentType:'video/mp4' } { 文件名:'a' }