我正在尝试从 multer
获取错误,但它给出了 no err
,尽管我尝试上传未被接受的文件。
这是代码
const storage = multer.diskStorage({
destination: (req, file, callback) => {
callback(null, '/public/uploads');
},
filename: (req, file, callback) => {
callback(null, file.fieldname + '-' + Date.now());
}
});
const upload = multer({
storage,
limits: {
fileSize: 1024 * 1024
},
fileSize: 15000000, // 150 KB for a 1080x1080 JPG 90
fileFilter: (req, file, cb) => {
if (
file.mimetype == 'image/png' ||
file.mimetype == 'image/jpg' ||
file.mimetype == 'image/jpeg' ||
file.mimetype == 'image/gif'
) {
console.log(file.mimetype);
cb(null, true);
} else {
cb(null, false);
return cb(new Error('not accepted'));
}
}
}).array('photos', 2);
app.post('/matches', async (req, res) => {
upload(req, res, function (err) {
if (err instanceof multer.MulterError) {
console.log(err);
// A Multer error occurred when uploading.
} else if (err) {
console.log(err);
// An unknown error occurred when uploading.
}
console.log('no er');
// Everything went fine.
});
}):
未触发 mimetype 错误.. 我不知道为什么?