multer: Multer is not populating req.body and req.file

I am using Multer to parse a multipart form in a keystone environment and and not able to access the req.body and req.file data inside my route controller

routes/index.js

var keystone = require('keystone'),
middleware = require('./middleware');

var bodyParser = require('body-parser');

//multi-part form parser for FTP api
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({storage: storage});

exports = module.exports = function(app) {
    app.use(bodyParser.json({limit: '10mb'}));
    app.use(bodyParser.urlencoded({limit: '10mb', extended: true}));
    app.post('/upload_api', upload.single('formFile'), routes.api.uploadFTP);
};

routes/api/uploadFTP.js

var keystone = require('keystone');
var ftpMod = require('ftp');
var fs = require('fs');

exports = module.exports = function(req, res) {
    console.log("req.body is ");
    console.log(req.body);
    console.log("req.file is ");
    console.log(req.file);
    res.send("console.log() outputted to screen");
}

public/test-upload.html

<html>
    <body>
        <form name="sampleForm" enctype="multipart/form-data" action="/upload_api" method="post">
            <p>Method</p>
            <input type="text" name="method"><br>
            <p>Options</p>
            <input type="text" name="options"><br>
            <p>File</p>
            <input type="file" name="formFile"><br><br>
            <input type="submit" value="Click to Send">
        </form>
    </body>
</html>

The response i receive from nodejs is

>req.body is 
{}
req.file is 
undefined

I am expecting req.body to contain {method: “sometext”} and req.file to be populated

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 27 (3 by maintainers)

Most upvoted comments

specifically, I noticed req.file is undefined while using upload.single() but req.files is populated.

This is at odds with the expected behavior according to the documentation

This is what I just discovered for my case.

In server.js I have

app.use(multer({dest:'./public/uploads/'}).any());

while my routes is using uploads.single. This is what caused req.file to be undefined but req.files to be populated.

After correcting the app.use() line in server.js to

app.use(multer({dest:'./public/uploads/'}).single('file'));

It begin to work as expected.

Yeah, I am still getting this issue. I installed the new bodyparser and multer middleware, but it won’t populate req.body in a multipart form. Anyone figure it out?

So, the right way is to use upload.single() ?. I don’t now right answer, how to parse both text and file. Help me, thanks.

https://github.com/musmanalibaloch/express-form-data, Use this example, it uses firebase cloud function and firebase storage to upload file but you can use it for anything , it uses express-multipart-file-parser and works like charm the above link just works perfectly,I tested this code and pushed it to repo for you guys.Just add your configuration and there you go.Cheers.

Yeah, that’s probably it. The first middleware will consume the request body, and then there will be nothing left in the stream to consume…