javascript 多部分/表单数据上传 - Nodejs - expressjs
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23027737/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multipart/form-data upload - Nodejs - expressjs
提问by nilveryboring
Since express.multipart is removed from the Express 4.x library, what will be the best way to handle file upload in expressjs?
由于 express.multipart 从 Express 4.x 库中删除,在 expressjs 中处理文件上传的最佳方法是什么?
采纳答案by kentcdodds
Just answered a similar questionabout multipart. I would recommend multiparty:
刚刚回答了一个关于 multipart的类似问题。我会推荐多方:
Have you given node-multipartya try? Here's example usage from the README:
你有没有尝试过节点多方?以下是 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) recommendsavoiding bodyParser, so you're right to avoid it, but multiparty seems to solve a similar issue for me.
作者 (Andrew Kelley)建议避免使用 bodyParser,因此您应该避免使用它,但 multiparty 似乎为我解决了类似的问题。
回答by Nono_Torres
You can use connect-multiparty
(https://github.com/andrewrk/connect-multiparty)
您可以使用connect-multiparty
(https://github.com/andrewrk/connect-multiparty)
It can be used as middleware in the routes you want to accept uploads.
它可以用作您要接受上传的路由中的中间件。