【问题标题】:In Nodejs (request) how to send file in a mutliPart http request在Node Js(请求)中如何在多部分http请求中发送文件
【发布时间】:2014-01-24 12:39:30
【问题描述】:

如何使用来自 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": {}
}

【问题讨论】:

  • 对“nodejs 多部分表单数据”的简单搜索揭示了这一点:github.com/felixge/node-form-data。请记住:Node.js 已经有大量可用的操作系统模块,只需搜索一下!
  • @SimonPlus 您可能应该提交该评论作为答案,并提及自述文件中的一个示例

标签: node.js http curl request


【解决方案1】:

对“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 已经有大量可用的操作系统模块,只需搜索一下!

【讨论】:

  • res.resume(); 在将我的项目升级到 0.10 后节省了我数小时的调试时间
猜你喜欢
  • 2016-05-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-08
  • 2012-10-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多