Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Express js Image upload and Text inputs using post method

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>
like image 296
Rijo Avatar asked Sep 01 '16 07:09

Rijo


1 Answers

Just put

console.log(req.body.user);
console.log(req.body.email);

inside your upload function. After the function may also work.

like image 98
Steven Winston Avatar answered Sep 20 '22 01:09

Steven Winston