如何通过http发送在node.js(multipart)中上传的文件,如下所示:
var options = {
host: url,
port: 8080,
path: '/sendFile',
method: 'POST'
};
http.request(options, function(res) {
console.log('STATUS: ' + res.statusCode);
console.log('HEADERS: ' + JSON.stringify(res.headers));
res.setEncoding('utf8');
res.on('data', function (chunk) {
console.log('BODY: ' + chunk);
});
}).end();
在http请求中,如何发送以多部分形式上传的文件?
答案 0 :(得分:0)
这取决于。有各种各样的策略。
最简单的方法是使用来自请求的.form()功能来正确使用,类似于:
var form = req.form();
form.append('file', file_content, {
filename: 'myfile.ext',
contentType: 'corresponding content/type'
});
然后执行帖子请求
...或者您可以流式传输并让请求提取所需的数据
form.append('file', fs.createReadStream('path/to/file'));