I'm using body-parser, express and multer
in my NodeJS app.
I need to upload Image and few text fields in signup form. I'm using multer
for this, I tried exactly the same thing suggested here
But I get empty object in req.body
.
Files are being created in the destination folder, but req.files.forEach
methods logs empty result.
Here is my code:
Html front end code
<form id="form" enctype="multipart/form-data" action="/profile" method="post" >
<label>Name</label>
<input type="text" placeholder=" Name" name="name" id="name" class="form-control">
<label>Logo</label>
<input type="file" placeholder="Logo" name="logo" id="logo" class="form-control">
<button id="addform" type="submit" class="btn btn-primary">Add Profile</button>
</form>
Server Side Code:
app.post('/profile', function(req, res) {
var storage = multer.diskStorage({
destination: __dirname+'/file/uploads/'
});
var upload = multer({ storage : storage}).any();
upload(req,res,function(err) {
if(err) {
console.log(err);
return res.end("Error uploading file.");
} else {
console.log(req.body);
console.log(req.files);
req.files.forEach( function(f) {
console.log(f);
// and move file to final destination...
});
res.end("File has been uploaded");
}
});
});
Log output in Node:
{}
[]