0
我需要在服務器上傳圖片,但如果它不是jpeg \ png,或者文件大小> 10Mb,我需要顯示一個錯誤。圖片上傳得很好,但是當我嘗試上傳.zip文件時,我的控制檯是空的,爲什麼我的代碼無法處理錯誤?如何處理與垂耳的錯誤?
var multer = require('multer');
var storage = multer.diskStorage({
destination: function (req, file, cb) {
cb(null, '../public/img/avatars')
},
filename: function (req, file, cb) {
cb(null, ''+req.user._id+'')
},
fileFilter: function (req, file, cb) {
if (file.mimetype !== 'image/png' && file.mimetype !== 'image/jpg' && file.mimetype !== 'image/gif') {
console.log('Wrong format!');
return cb(null, false, new Error('Wrong format!'));
}
if (file.size > 10000){
console.log('Too large!');
return cb(null, false, new Error('Too large!'));
}
cb(null, true);
}
});
var upload = multer({ storage: storage });
router.post('/changeAvatar', upload.single('avatar'), function(req, res) {
var id = req.user._id;
res.redirect('/user/'+id);
});