8

Since express.multipart is removed from the Express 4.x library, what will be the best way to handle file upload in expressjs?

3
  • 2
    Alternatives are listed in Connect's documentation: github.com/senchalabs/connect#middleware Commented Apr 12, 2014 at 7:57
  • @JonathanLonowski yea but these libraries looks not as clean as the old one, which one do u prefer? Commented Apr 13, 2014 at 2:24
  • Technically many of those came from the old one. connect-multiparty is more or less that one you need. Commented Apr 14, 2014 at 13:09

2 Answers 2

6

Just answered a similar question about multipart. I would recommend multiparty:

Have you given node-multiparty a try? Here's example usage from the README:

var multiparty = require('multiparty')
  , http = require('http')
  , util = require('util')

http.createServer(function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    // parse a file upload
    var form = new multiparty.Form();

    form.parse(req, function(err, fields, files) {
      res.writeHead(200, {'content-type': 'text/plain'});
      res.write('received upload:\n\n');
      res.end(util.inspect({fields: fields, files: files}));
    });

    return;
  }

  // show a file upload form
  res.writeHead(200, {'content-type': 'text/html'});
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);

The author (Andrew Kelley) recommends avoiding bodyParser, so you're right to avoid it, but multiparty seems to solve a similar issue for me.

Sign up to request clarification or add additional context in comments.

3 Comments

is there a way to use this as a middleware? I dont want to include this everywhere
@nilveryboring I'm not sure what you mean... You could definitely put this somewhere that could be reused... I don't know why you'd need this everywhere....
Ok.. I have tried this our as well. How do I get the file without setting the response header?? I want to do other process then if ok.. set the response header or set error back. res.end(util.inspect({fields: fields, files: files})); ..I just want the file sent but leaving the header alone???
0

You can use connect-multiparty (https://github.com/andrewrk/connect-multiparty)

It can be used as middleware in the routes you want to accept uploads.

1 Comment

In connect-multiparty README, it says to use multiparty instead so this doesn't seem to be a valid alternative to the above question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.