如何使用来自Nodejs的MultiPart使用请求发送文件?
这与Curl的以下命令相同:
curl -X POST -F "photos[]=@img.jpg;type=image/jpg" https://example.com
它创建的Http请求:
{
"json": null,
"files": {
"photos[]": "data:image/jpg;base64,[Binary]
},
"form": {},
"headers": {
"Accept": "*/*",
"Content-Type": "multipart/form-data; boundary=----------------------------0195fbe0d4ab",
"Connection": "close",
"Host": "httpbin.org",
"Content-Length": "42311",
"User-Agent": "curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8x zlib/1.2.5"
},
"origin": "23.125.128.191",
"data": "",
"url": "http://httpbin.org/post",
"args": {}
}
答案 0 :(得分:2)
对“nodejs多部分表单数据”的简单搜索显示了这一点:github.com/felixge/node-form-data。它甚至适用于request模块:
var FormData = require('form-data');
var request = require('request');
var form = new FormData();
form.append('photos[]', request('img.jpg'));
form.submit('https://example.com/', function(err, res) {
res.resume(); // for node-0.10.x
});
请记住:Node.js已经有大量的OS模块可供使用,只需搜索一下!