node.js - stop the file upload in multer if the user validation fails -


the file uploading done multer using code, how stop file upload when user validation fails. write user validation part in code

router.post('/profilepicture',  multer({dest: './uploads/', rename: function (fieldname, filename,req,res) {       return image = req.body.userid+'-'+datetime+'-'+randomid();     },     onfileuploadstart: function (file,req,res) {         if(file.mimetype !== 'image/jpg' && file.mimetype !== 'image/jpeg' && file.mimetype !== 'image/png') {           imageuploaddone = false;           return false;         }         //console.log(file.originalname + ' starting ...');     },     onfileuploadcomplete: function (file,req,res) {       //console.log(file.fieldname + ' uploaded  ' + file.path);       if(file.mimetype == 'image/jpg')         extn  = '.jpg';       if(file.mimetype == 'image/jpeg')         extn  = '.jpeg';       if(file.mimetype == 'image/png')         extn  = '.png';       imageuploaddone=true;      } }),function(req, res) {        upload(req,res,function(err) {     if(imageuploaddone==true){       //console.log(image);       var userinfo = {'userid':req.body.userid,'newimage':address+image+extn,'path':'./uploads/'};           db.profilepicture(userinfo,function(result){             if(result.message == 'image path added'){               res.json({'success':'1','result':{'message':'profile picture updated','imageurl':address+image+extn},'error':'no error'});             }           });     }     if(imageuploaddone == false){     res.json({'success':'0','result':{},'error':'file format not supported'});   }   }); }); 

i try validate user on events onfileuploadstart , onfileuploadcomplete. if user not valid still file gets uploaded path.

this possible in 1.0.0.

if want abort upload:

multer({       filefilter: function (req, file, cb) {          if (path.extension(file.originalname) !== '.pdf') {                  return cb(new error('only pdfs allowed'))           }           cb(null, true)        }  }) 

if want skip files not pdf:

multer({       filefilter: function (req, file, cb) {           if (path.extension(file.originalname) !== '.pdf') {                   return cb(null, false)        }        cb(null, true)       }  }) 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -