multer icon indicating copy to clipboard operation
multer copied to clipboard

Multer is not populating req.body and req.file

Open nickanna42 opened this issue 9 years ago • 27 comments

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

nickanna42 avatar Jun 21 '17 15:06 nickanna42

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

nickanna42 avatar Jun 21 '17 18:06 nickanna42

Having same issue with this version in my package.json "multer": "^1.3.0".

All code that i see from Stack Overflow use req.file in conjunction with upload.single().

So something must have change to cause this.

HarisHashim avatar Oct 01 '17 13:10 HarisHashim

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.

HarisHashim avatar Oct 01 '17 14:10 HarisHashim

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...

LinusU avatar Oct 02 '17 09:10 LinusU

@LinusU @HarisHashim Can you please post the correct code here

t1war1 avatar Dec 24 '17 16:12 t1war1

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.

MarcelGeo avatar Jan 21 '18 00:01 MarcelGeo

Hi, If you are still interested, my issue was that multer was the second middleware and req.file was empty. After moving multer to be the first middleware, everything worked.

lordvee avatar May 02 '18 21:05 lordvee

I have the same problem, if only the file is uploaded then it uploads smoothly but if the form has a file and some fields then req.body is empty and req.files is undefined but it works using postman though :l

saadnaseem97 avatar May 07 '18 21:05 saadnaseem97

@saadnaseem97 are you using any other body-parsing middleware? How does your request look in postman?

LinusU avatar May 08 '18 09:05 LinusU

Is there is any way to update multiple image in multer??? My upload code works file.. comes to update i got this error " Cannot read property 'filename' of undefined"

vigneshkvw avatar May 11 '18 04:05 vigneshkvw

@vigneshkvw how are you reading filename? If you have multiple files you can no longer read req.file.filename, but must instead iterate over the req.files property:

for (const file of req.files) {
  console.log(file.filename)
}

LinusU avatar May 13 '18 09:05 LinusU

@saadnaseem97 can you show how you uploaded file directly without using formdata ?

marypreethi12 avatar Jun 12 '18 13:06 marypreethi12

@marypreethi12 What do you mean by directly? If you plan on using an html form to upload a file, it is best practices to set the method to multipart/form-data. The get and post methods do not support file transfer directly.

It would be possible to use the File API in the browser to convert the the file to base64 string and send this string in a form field with the get or post methods.

See this link for an walkthrough of applicable API.

nickanna42 avatar Jun 12 '18 14:06 nickanna42

@vigneshkvw Please help me i am facing the same issue.Did you solve this...?

ghost avatar Jun 13 '18 13:06 ghost

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?

eoriont avatar Sep 01 '18 03:09 eoriont

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.

musmanalibaloch avatar Sep 17 '18 07:09 musmanalibaloch

I am also experiencing this for multi file uploads. For some reason a single file works as expected. I have an open question on stackoverflow. My code is nearly identical to the single file upload but the req.files end up empty and can be found in req.body. Here is the question if anyone is interested in helping me with this. I've gone through solutions on this page and everything I could find on stackoverflow.

ghost avatar Oct 26 '18 12:10 ghost

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.

@FrancoTanzarella did you look at it? I had the same issue wasted a lot of time and then after solving shared that on git

musmanalibaloch avatar Oct 26 '18 12:10 musmanalibaloch

@musmanalibaloch - I did try that, although only the parts I thought were relevant.

const { fileParser} = require('express-multipart-file-parser'); app.use(fileParser({ rawBodyOptions: { limit: '15mb', //file size limit }, busboyOptions: { limits: { fields: 20 //Number text fields allowed } }, }))

Am I missing something?

ghost avatar Oct 26 '18 13:10 ghost

@FrancoTanzarella After that try something like this with some dummy route app.post('/v1/image', (req, res) => { if (req.files) { //check if images array is populated //access images like req.files[index] where index starts from 0 //req.files[0] first file //req.files[1] second file ...

})

use data-form to send images to server , you can use postman to test

musmanalibaloch avatar Oct 26 '18 16:10 musmanalibaloch