Bodyparser Parse Data Instead Of Connect-multiparty In Node.js
I am trying to upload file using connect-multiparty with reference of connect-multiparty below is my express.js config for that. app.use(bodyParser.urlencoded({ extended:
Solution 1:
You can use node-formidable module to handle multipart form data:
var formidable = require('formidable');
app.post('/upload', function(req, res, next) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
console.log(fields);
console.log(files);
res.send('done');
});
});
Solution 2:
Use either bodyParser or connect-multiparty to parse request. Both cannot be used at same time. You can parse json with connect-multiparty than why use bodyParser but bodyParser cannot parse multipart forms so we need to use other parsers like connect-multparty. see this link.
Solution 3:
index.html code
<html><body><divid="main-content"><formaction="upload"method="post"enctype="multipart/form-data"><inputtype="text"name="FirstName" ><br><inputtype="text"name="LastName" ><br><inputtype="submit"></div></div></body></html>
server.js
var express = require('express');
var app = express();
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.use("/", express.static(__dirname + '/client/'));
app.post('/upload', multipartMiddleware, function(req, resp) {
console.log(req);
console.log(req.body);
// don't forget to delete all req.files when done
});
app.listen(3000,function(){
console.log("App Started on PORT 3000");
});
Post a Comment for "Bodyparser Parse Data Instead Of Connect-multiparty In Node.js"