Express and Multer upload issue - req.files is always empty Express and Multer upload issue - req.files is always empty express express

Express and Multer upload issue - req.files is always empty


OK I was struggling with this for hours - I finally managed to get it working.

The problem turned out to be that I had to add a 'name' attribute to my <input type='file' /> tag, like this <input type='file' name='myFileName'>

Then also - I had to make sure that in my multer setup - I had to have exactly the same name 'myFileName' in my array() function call - like this:

router.post('/', multer({dest:'./uploads'}).array('myFileName', 1), function(req, res, next) {});

This happened to fix the problem..

Hope this helps!


To get req.file correct, first you have to set the parameters for .post() in correct way. Use it as:

router.route('/user/signmeup').post(upload.array('profilePicture', 2),function(req, res){        console.dir(req.headers['content-type']);        if (err) {            console.log('err', err);        }else{            console.log('coming here');            console.log(req.files);            console.log(req.body);            var reg = new Registration();            let args = { body : req.body, session : req.session };            reg.applyForMembership(args, function(err, result){            res.json(result);            });        }});