Sorry for such a noob question, but I have a form upload images and add some text to the DB, the images are uploading fine, but the req.body object is always an empty array.
HTML
<form class='new-project' action='/projects' method='POST' enctype="multipart/form-data">
<input type="text" name="title" placeholder="Project Title">
<br>
<textarea name="description" rows="8" cols="40" placeholder="Project description"></textarea>
<br>
<label for='file'>Select your image:</label>
<input type='file' accept='image/*' name='uploadedImages' multiple/>
<span class='hint'>Supported files: jpg, jpeg, png.</span>
<br>
<input type="submit" value="uploading_img">
</form>
JS
var bodyParser = require('body-parser'),
express = require('express'),
multer = require('multer'),
var storage = multer.diskStorage({
destination: function (request, file, callback) {
callback(null, './public/uploads');
},
filename: function (request, file, callback) {
console.log(file);
callback(null, file.originalname)
}
});
var upload = multer({storage: storage}).any('uploadedImages');
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.post('/projects', function(req, res){
console.log('req.body');
console.log(req.body);
upload(req, res, function(err){
if(err){
console.log('Oh dear...');
console.log(err);
return;
}
console.log(req.files);
res.end('Your files uploaded!');
console.log('Yep yep!');
});
});
According to the documentation, multer add a body object to the request, but I only get an empty array back with or without body-parser.
Thanks for taking the time to look through the code!!
Your can get desired
req.body
inside the upload() method. Try loggingreq.body
in the place where you printreq.files
.