当文件被拒绝时,使用multer投掷错误上传多个文件上传
我正在使用.fields上传两个图像([{name:'centerripimage',maxcount:1},{name:'reskaldProofImage',maxcount:1}]);
,但是当FileFilter拒绝文件
错误
if (this.req.files[placeholder.fieldname].length === 1) {
^
TypeError: Cannot read properties of undefined (reading 'length')
multer投掷错误时,在文件appender.js中的第49行时,它会丢弃错误。 req.files [占位符。fieldname]显示未定义。
case 'OBJECT':
if (this.req.files[placeholder.fieldname].length === 1) {
delete this.req.files[placeholder.fieldname]
} else {
arrayRemove(this.req.files[placeholder.fieldname], placeholder)
}
break
我的代码
const multer = require('multer');
const path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log('type',req.body.type);
cb(null, "uploads/images/");
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
},
});
const maxSize = 1 * 1024 * 1024; // for 1MB
var upload = multer({
storage: storage,
fileFilter: async (req, file, cb) => {
var ext = path.extname(file.originalname);
console.log("ext", ext);
let validImageExtensions = ['png', 'jpg', 'jpeg'];
if (validImageExtensions.indexOf(ext.substring(1)) === -1) {
cb(null, false);
console.log("invalid extension");
return cb(new Error("Only " + validImageExtensions + " are allowed with maxsize 1MB"));
}else{
console.log("valid extension");
cb(null, true);
}
},
limits: { fileSize: maxSize },
});
module.exports = upload;
如果文件扩展名匹配和图像存储在上传文件夹中,则一切正常,但是当扩展不匹配时,会丢弃相同的错误。
我为您创建了GitHub存储库,以更好地测试错误。 https://github.com/alexadvent/multer-multer-multer-multer-multiple-multiple-sissue
并发送put put put put put在URL Localhost请求:运行服务后8000/上传图像。
I am uploading two image using .fields([{ name: 'certificateImage', maxCount: 1 }, { name: 'addressProofImage', maxCount: 1 }]);
but it is throwing error when filefilter reject the file
error
if (this.req.files[placeholder.fieldname].length === 1) {
^
TypeError: Cannot read properties of undefined (reading 'length')
Multer throw error at line 49 in file-appender.js. req.files[placeholder.fieldname] is showing undefined.
case 'OBJECT':
if (this.req.files[placeholder.fieldname].length === 1) {
delete this.req.files[placeholder.fieldname]
} else {
arrayRemove(this.req.files[placeholder.fieldname], placeholder)
}
break
My code
const multer = require('multer');
const path = require('path');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log('type',req.body.type);
cb(null, "uploads/images/");
},
filename: function (req, file, cb) {
cb(
null,
file.fieldname + "-" + Date.now() + path.extname(file.originalname)
);
},
});
const maxSize = 1 * 1024 * 1024; // for 1MB
var upload = multer({
storage: storage,
fileFilter: async (req, file, cb) => {
var ext = path.extname(file.originalname);
console.log("ext", ext);
let validImageExtensions = ['png', 'jpg', 'jpeg'];
if (validImageExtensions.indexOf(ext.substring(1)) === -1) {
cb(null, false);
console.log("invalid extension");
return cb(new Error("Only " + validImageExtensions + " are allowed with maxsize 1MB"));
}else{
console.log("valid extension");
cb(null, true);
}
},
limits: { fileSize: maxSize },
});
module.exports = upload;
everything work fine if file extension match and image store in upload folder but throw same error when extension doesnt not match.
I have created github repo for the you to better to test error.
https://github.com/AlexAdvent/multer-multiple-issue
and send put request at url localhost:8000/upload-image after running serve.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论