node.js - Cant get multer filefilter error handling to work -


im playing around file uploading in node.js/multer

i got the storage , limits working. im playing around filefilter deny files mimetype this:

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

when file gets uploaded not png wont accept it. wont trigger if(err)

when file big generate error. somehow need generate err on filefilter aswell im not sure how , guess new error wrong

so how supposed generate error if file not correct. 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 clue!'));      }      cb(null, true);    }   }).single('bestand');   router.post('/upload',function(req,res){     upload(req,res,function(err) {         if(err) {               return res.end("some error");         }     )} )} 

filefilter function has access request object (req). object available in router.

therefore in filefitler can add property validation error or validation error list (you can upload many files, , of them pass). , in router check if property errors exists.

in filter:

filefilter: function (req, file, cb) {  if (file.mimetype !== 'image/png') {   req.filevalidationerror = 'goes wrong on mimetype';   return cb(null, false, new error('goes wrong on 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);         }     )} )} 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -