在multer方面,我使用了一些过滤并有一些限制:
var photoUpload = multer({ storage: phtStorage, fileFilter: photoFilter, limits: limits }).single('file');
但它们并非一帆风顺。
过滤器:
var photoFilter = function(req, file, cb) {
if (settings.acceptedPhotoTypes.indexOf(file.mimetype) !== -1)
cb(null, true);
else
cb('Invalid file type', false);
};
存储
var phtStorage = multer.diskStorage({
destination: function (req, file, cb) {
var path = settings.photoProcessingSrcDir;
mkdirp(path, err => cb(err, path));
cb(null, path);
},
filename : function (req, file, cb) {
cb(null, req.fileName.toString());
}
});
限制:
var limits = { fileSize: 20000000 };
我唯一能做的就是将函数photoUpload
传递给另一个函数,因为我想在实际上传之前做一些数据库验证。
router.post('/:catalogId/:storeId', protected, function(req, res, next) {
if (f.type === 'photo') {
require('../helpers/files/post-photo')(photoUpload, req, res, next);
}
});
答案 0 :(得分:0)
我终于找到了问题。存储功能写得不好:
var phtStorage = multer.diskStorage({
destination: function (req, file, cb) {
var path = settings.photoProcessingSrcDir;
----> mkdirp(path, err => cb(err, path));
----> cb(null, path);
},
filename : function (req, file, cb) {
cb(null, req.fileName.toString());
}
});
调用cb(回调函数)并导致multers内部流创建函数创建两个流。这真的搞砸了......