sri*_*hks 65
对不起,我无法对Scott上面的答案发表评论,但我有一个解决方法,可以添加正确的文件扩展名.如果使用path节点模块
var multer = require('multer');
var path = require('path')
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, 'uploads/')
},
filename: function (req, file, cb) {
cb(null, Date.now() + path.extname(file.originalname)) //Appending extension
}
})
var upload = multer({ storage: storage });
Run Code Online (Sandbox Code Playgroud)
Sco*_*ott 35
从文档:"Multer不会为您附加任何文件扩展名,您的函数应该返回一个带有文件扩展名的文件名."
以下是添加扩展程序的方法:
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 });
Run Code Online (Sandbox Code Playgroud)
我建议使用该mimetype属性来确定扩展名.例如:
filename: function (req, file, cb) {
console.log(file.mimetype); //Will return something like: image/jpeg
Run Code Online (Sandbox Code Playgroud)
更多信息:https://github.com/expressjs/multer
小智 9
我收到了扩展文件file.mimetype.我拆分mimetype并从中获取文件扩展名请尝试以下功能.
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 })
Run Code Online (Sandbox Code Playgroud)
可以这样做:
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, config.DIR)
},
filename: function (req, file, cb) {
let ext = file.originalname.substring(file.originalname.lastIndexOf('.'), file.originalname.length);
cb(null, Date.now() + ext)
}
});
const upload = multer({
storage: storage
}).any();
Run Code Online (Sandbox Code Playgroud)
我使用这个小技巧来获取文件扩展名,并作为解决方法来避免有人上传具有相似文件名的文件两次或服务器中存在的文件时可能出现的问题。
const path = require('path');
const crypto = require('crypto');
let upload = multer({
storage: multer.diskStorage({
destination: (req, file, cb) => {
cb(null, path.join(__dirname, '../uploads'))
},
filename: (req, file, cb) => {
// randomBytes function will generate a random name
let customFileName = crypto.randomBytes(18).toString('hex')
// get file extension from original file name
let fileExtension = path.extname(file.originalname).split('.')[1];
cb(null, customFileName + '.' + fileExtension)
}
})
})
Run Code Online (Sandbox Code Playgroud)
import multer from 'multer';
import * as shortid from 'shortid';
import * as mime from 'mime-types';
const storage = multer.diskStorage({
destination: function (req,file,cb) {
cb(null, '/path/to/uploads/');
},
filename: function (req,file,cb) {
/* generates a "unique" name - not collision proof but unique enough for small sized applications */
let id = shortid.generate();
/* need to use the file's mimetype because the file name may not have an extension at all */
let ext = mime.extension(file.mimetype);
cb(null, `${id}.${ext}`);
}
});
Run Code Online (Sandbox Code Playgroud)
编辑
shortid 已被弃用,您应该使用 nanoid。
import multer from 'multer';
import * as nanoid from 'nanoid';
import * as mime from 'mime-types';
const storage = multer.diskStorage({
destination: function (req,file,cb) {
cb(null, '/path/to/uploads/');
},
filename: function (req,file,cb) {
/* generates a "unique" name - not collision proof but unique enough for small sized applications */
let id = nanoid();
/* need to use the file's mimetype because the file name may not have an extension at all */
let ext = mime.extension(file.mimetype);
cb(null, `${id}.${ext}`);
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
31415 次 |
| 最近记录: |