问题描述
任何人都知道为什么重命名"功能(以及所有其他multer回调)不起作用吗?
Anyone knows why the "rename" function (and all other multer callbacks) are not working?
var express = require('express');
var multer = require('multer');
var app = express();
app.use(multer({
dest: 'uploads/',
rename: function (fieldname, filename) {
return new Date().getTime();
},
onFileUploadStart: function (file) {
console.log(file.name + ' is starting ...');
},
onFileUploadComplete: function (file, req, res) {
console.log(file.name + ' uploading is ended ...');
console.log("File name : "+ file.name +"\n"+ "FilePath: "+ file.path)
},
onError: function (error, next) {
console.log("File uploading error: => "+error)
next(error)
},
onFileSizeLimit: function (file) {
console.log('Failed: ', file.originalname +" in path: "+file.path)
fs.unlink(path.join(__dirname, '../tmpUploads/') + file.path) // delete the partially written file
}
}).array('photos', 12));
app.listen(8080,function(){
console.log("Working on port 8080");
});
app.get('/',function(req,res){
res.sendFile(__dirname + "/index.html");
});
app.post('/photos/upload', function (req, res, next) {
// req.files is array of `photos` files
// req.body will contain the text fields, if there were any
//console.log(req.files);
//console.log(req.body);
res.json(req.files)
});
推荐答案
似乎用法已随着时间而改变.当前,multer
构造函数仅接受以下选项( https://www.npmjs.com/package/multer#multer -opts ):
It seems the usage has been changed over time. Currently, multer
constructor only accepts following options (https://www.npmjs.com/package/multer#multer-opts):
-
dest
或storage
-文件存储位置 -
fileFilter
-控制接受哪些文件的功能 -
limits
-上载数据的限制
dest
orstorage
- Where to store the filesfileFilter
- Function to control which files are acceptedlimits
- Limits of the uploaded data
因此,例如,重命名将通过配置适当的存储来解决( https://www.npmjs.com/package/multer#storage ).
So, for example the renaming is to be solved by configuring appropriate storage (https://www.npmjs.com/package/multer#storage).
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '/tmp/my-uploads'); // Absolute path. Folder must exist, will not be created for you.
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now());
}
})
var upload = multer({ storage: storage });
app.post('/profile', upload.single('fieldname'), function (req, res, next) {
// req.body contains the text fields
});
fieldname
必须与请求正文中的字段名称匹配.也就是说,在HTML表单发布的情况下,表单上传元素的输入名称.
The fieldname
must match the field name in the request body. That is, in case of HTML form post, the form upload element input name.
还可以查看其他中间件功能,例如array
和fields
- https://www. npmjs.com/package/multer#single-fieldname ,它提供了一些不同的功能.
Also have a look for other middleware functions like array
and fields
- https://www.npmjs.com/package/multer#single-fieldname which provide a a little different functionality.
您可能还会对限制感兴趣( https://www.npmjs.com/package/multer#limits )和文件过滤器( https://www.npmjs.com/package/multer#filefilter )
Also you may be interested in the limits (https://www.npmjs.com/package/multer#limits) and file filter (https://www.npmjs.com/package/multer#filefilter)
而且-来源是真理的唯一来源-看看吧!( https://github .com/expressjs/multer/blob/master/index.js )
And also - source is the single source of truth - have a peek!(https://github.com/expressjs/multer/blob/master/index.js)
这篇关于multer回调无法正常工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!