I have following code for uploading image and handling text input. But both are not working together *Images are uploading properly but I can't get text field value *
var express = require('express');
var multer = require('multer');
var mime = require('mime');
var app = express();
var bodyParser = require("body-parser");
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
},
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' + Date.now() + '.' + mime.extension(file.mimetype));
}
});
var upload = multer({ storage : storage }).array('userPic');
app.get('/completeForm.html', function(req, res){
res.sendFile(__dirname + '/' + 'completeForm.html')
});
app.post("/postFormAct", function (req, res, next) {
console.log(req.body.user); // Here i getting undefined
console.log(req.body.email);// Here i getting undefined
upload(req,res,function(err) {
console.log(req.files); // Here i getting proper output and image also uploading to concern folder
});
});
app.listen(3000);
Html code given below.... please help me someone
<form method="post" action="/postFormAct" enctype="multipart/form-data">
<input type="text" name="user"><br>
<input type="text" name="email"><br>
<input type="file" name="userPic"><br>
<input type="submit" value="Submit">
</form>
Just put
console.log(req.body.user);
console.log(req.body.email);
inside your upload function. After the function may also work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With