I'm playing around with file uploading in Node.js/Multer.
I got the the storage and limits working. But now im playing around with filefilter to simply deny some files by mimetype
like this:
fileFilter: function (req, file, cb) {
if (file.mimetype !== 'image/png') {
return cb(null, false, new Error('goes wrong on the mimetype'));
}
cb(null, true);
}
When a file gets uploaded that is not a PNG it wont accept it.
But it also won't trigger the if(err)
When the file is to big it does generate the error. So somehow I need to generate an err on filefilter
as well but im not sure how and guess new Error
is wrong.
So, how am I supposed to generate an error if the file is not correct? What am I doing wrong?
Full code:
var maxSize = 1 * 1000 * 1000;
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, 'public/upload');
},
filename: function (req, file, callback) {
callback(null, file.originalname);
}
});
var upload = multer({
storage : storage,
limits: { fileSize: maxSize },
fileFilter: function (req, file, cb) {
if (file.mimetype !== 'image/png') {
return cb(null, false, new Error('I don\'t have a clue!'));
}
cb(null, true);
}
}).single('bestand');
router.post('/upload',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end("some error");
}
)}
)}