Can't get multer filefilter error handling to work

Merijn de Klerk picture Merijn de Klerk · Jan 28, 2016 · Viewed 19.8k times · Source

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");
        }
    )}
)}

Answer

Krzysztof Sztompka picture Krzysztof Sztompka · Jan 28, 2016

fileFilter function has access to request object (req). This object is also available in your router.

Therefore in fileFitler you can add property with validation error or validation error list (you can upload many files, and some of them could pass). And in router you check if property with errors exists.

in filter:

fileFilter: function (req, file, cb) {
 if (file.mimetype !== 'image/png') {
  req.fileValidationError = 'goes wrong on the mimetype';
  return cb(null, false, new Error('goes wrong on the mimetype'));
 }
 cb(null, true);
}

in router:

router.post('/upload',function(req,res){
    upload(req,res,function(err) {
        if(req.fileValidationError) {
              return res.end(req.fileValidationError);
        }
    )}
)}