Since express.multipart is removed from the Express 4.x library, what will be the best way to handle file upload in expressjs?
-
2Alternatives are listed in Connect's documentation: github.com/senchalabs/connect#middlewareJonathan Lonowski– Jonathan Lonowski2014-04-12 07:57:15 +00:00Commented Apr 12, 2014 at 7:57
-
@JonathanLonowski yea but these libraries looks not as clean as the old one, which one do u prefer?nilni– nilni2014-04-13 02:24:09 +00:00Commented 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.Farid Nouri Neshat– Farid Nouri Neshat2014-04-14 13:09:52 +00:00Commented Apr 14, 2014 at 13:09
Add a comment
|
2 Answers
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.
3 Comments
nilni
is there a way to use this as a middleware? I dont want to include this everywhere
kentcdodds
@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....
philipfwilson
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???
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
Arsal
In connect-multiparty README, it says to use multiparty instead so this doesn't seem to be a valid alternative to the above question