-1

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: true
    }));
    app.use(bodyParser.json());

    //file upload configuration
    app.use(multipart({
            uploadDir: config.tmp
    }));

but when I upload file than it gives me request size if too long. I search for this and found that I need to set limit so I have also put limit parameter like below:

app.use(bodyParser.json({limit:'50mb'}));

but after that I start getting Invalid json error. than I found that bodyParser could not parse multi-part data. but i don't know how can i use multipart middleware to parse multi-part data.

Ilesh Patel
  • 2,053
  • 16
  • 27

3 Answers3

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');
  });
});
Benny Bottema
  • 11,111
  • 10
  • 71
  • 96
Kevin
  • 546
  • 4
  • 17
0

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.

KlwntSingh
  • 1,084
  • 8
  • 26
0

index.html code

<html><body>
<div id="main-content">
    <form action="upload" method="post" enctype="multipart/form-data">
         <input type="text" name="FirstName" ><br>
             <input type="text" name="LastName" ><br>
            <input type="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");
});
KlwntSingh
  • 1,084
  • 8
  • 26