本文介绍了如何使用multer存储带有文件扩展名的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
设法将我的文件存储在一个文件夹中,但是它们存储时没有文件扩展名.
Managed to store my files in a folder but they store without the file extension.
有人知道我将如何存储带有文件扩展名的文件吗?
Does any one know how would I store the file with file extension?
推荐答案
从文档中:"Multer不会为您添加任何文件扩展名,您的函数应返回带有文件扩展名的文件名."
From the docs: "Multer will not append any file extension for you, your function should return a filename complete with an file extension."
以下是添加扩展名的方法:
Here's how you can add the extension:
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + '.jpg') //Appending .jpg
}
})
var upload = multer({ storage: storage });
我建议使用mimetype
属性来确定扩展名.例如:
I would recommend using the mimetype
property to determine the extension. For example:
filename: function (req, file, cb) {
console.log(file.mimetype); //Will return something like: image/jpeg
更多信息: https://github.com/expressjs/multer
这篇关于如何使用multer存储带有文件扩展名的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!