req.body will always empty when we use multer?
See original GitHub issueHi guys sorry for the noob question but I was trying to upload file from angular js using https://github.com/danialfarid/ng-file-upload. The problem is when I multer the req.body will always empty. Is this normal? if this is normal how could I send file with some data to name it like [data.id].[mimetype] Something like this
Thank you for your patience in reading my issue here is my Request Payload and code on server side
Request Payload
------WebKitFormBoundarykUG7UcxeBDOSFFb1
Content-Disposition: form-data; name="file"; filename="AD ShoppingisGREATedit.jpg"
Content-Type: image/jpeg
------WebKitFormBoundarykUG7UcxeBDOSFFb1
Content-Disposition: form-data; name="email"
xx@xxzc.com
------WebKitFormBoundarykUG7UcxeBDOSFFb1
Content-Disposition: form-data; name="classId"
76
------WebKitFormBoundarykUG7UcxeBDOSFFb1--
At my Server side
router.post('/upload', function(req, res, next){
console.log('uploading---')
console.log(req)
var storage = multer.diskStorage({
destination: function (req, file, cb) {
console.log(req.body)
cb(null, ('hidden/images/slip/' +req.body.classId) )
},
filename: function (req, file, cb) {
cb(null, file.fieldname + '-' + Date.now())
}
})
//var upload = multer({ dest: ('hidden/images/slip/' + req.body.classId) }).single('file')
var upload = multer({ storage:storage }).single('file')
console.log(upload)
upload(req,res,function(err) {
if(err) {
return handleError(err, res);
}
console.log("done upload---")
res.json({"status":"completed"});
});
})
Issue Analytics
- State:
- Created 8 years ago
- Reactions:1
- Comments:13 (2 by maintainers)
Top Results From Across the Web
Node.js, multer and req.body empty - Stack Overflow
I am using multer to upload multiple files and single file in my nodejs ... next ) => { console.log( req.body ); //...
Read more >@neuralegion/multer - npm
Multer is a node.js middleware for handling multipart/form-data , which is primarily used for uploading files. It is written on top of busboy ......
Read more >Multer: Easily upload files with Node.js and Express
The code in the image above means that the req.body object is empty, which is to be expected. If you'll recall, body-parser ...
Read more >[Solved]-Node.js, multer and req.body empty-node.js
The default express body-parser cannot work with multipart/form-data, hence we use multer to parse form-data which is accessible inside your upload function.
Read more >Express body-parser middleware
This error will occur when the content of the request exceeds the configured parameterLimit for the urlencoded parser. The status property is set...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
From the documentation:
As you can see in your request payload, the file is sent before the
classId
field, and thus there is no way for multer to know aboutclassId
when it’s handling the file.I wish I had an easy solution for you but the only thing I can recommend is to manually reorder the fields with javascript to make sure that fields are sent before files.
This has come up a number of times before, e.g. #146 where the workaround is outlined…
It doesn’t work for me. but i found the real cause is, we should print req.body inside the upload() function, instead of outside of it (either before or after it).
Try adding below line
console.log(req.body)
nearconsole.log("done upload---")
in above code example, it should print all non-file type form fields .