如何在请求节点获取或节点中发送文件? [英] How to send a file in request node-fetch or Node?

查看:102
本文介绍了如何在请求节点获取或节点中发送文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Node或Node Fetch POST请求中附加文件?我正在尝试调用将导入CSV或XLS文件的API.使用Node或Node Fetch可以做到这一点吗?

How do I attach a file in Node or Node Fetch POST request? I am trying to invoke an API which will import a CSV or XLS file. Is this possible using Node or Node Fetch?

推荐答案

在请求和响应上都使用本机流作为正文.

Use native stream for body, on both request and response.

来源表示它支持几种类型,例如StreamBufferBlob ...,并且还会尝试将其他类型强制为String.

And sources indicate it supports several types, like Stream, Buffer, Blob... and also will try to coerce as String for other types.

以下代码段显示了3个示例,所有示例均适用于v1.7.1或2.0.0-alpha5(另请参见使用FormData的其他示例):

Below snippet shows 3 examples, all work, with v1.7.1 or 2.0.0-alpha5 (see also other example further down with FormData):

let fetch = require('node-fetch');
let fs = require('fs');

const stats = fs.statSync("foo.txt");
const fileSizeInBytes = stats.size;

// You can pass any of the 3 objects below as body
let readStream = fs.createReadStream('foo.txt');
//var stringContent = fs.readFileSync('foo.txt', 'utf8');
//var bufferContent = fs.readFileSync('foo.txt');

fetch('http://httpbin.org/post', {
    method: 'POST',
    headers: {
        "Content-length": fileSizeInBytes
    },
    body: readStream // Here, stringContent or bufferContent would also work
})
.then(function(res) {
    return res.json();
}).then(function(json) {
    console.log(json);
});

这里是foo.txt:

hello world!
how do you do?

注意:http://httpbin.org/post使用JSON答复,该JSON包含有关已发送请求的详细信息.

Note: http://httpbin.org/post replies with JSON that contains details on request that was sent.

结果:

{
  "args": {}, 
  "data": "hello world!\nhow do you do?\n", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Accept": "*/*", 
    "Accept-Encoding": "gzip,deflate", 
    "Connection": "close", 
    "Content-Length": "28", 
    "Host": "httpbin.org", 
    "User-Agent": "node-fetch/1.0 (+https://github.com/bitinn/node-fetch)"
  }, 
  "json": null, 
  "origin": "86.247.18.156", 
  "url": "http://httpbin.org/post"
}

如果您需要发送带有更多参数的文件作为表单的一部分,则可以尝试:

If you need to send a file as part of a form with more parameters, you can try:

  • npm install form-data
  • FormData对象作为主体传递(FormDataStream的一种,通过)
  • 请勿在选项中传递header(与上述示例不同)
  • npm install form-data
  • pass a FormData object as body (FormData is a kind of Stream, via CombinedStream library)
  • do not pass header in the options (unlike examples above)

然后可行:

const formData = new FormData();
formData.append('file', fs.createReadStream('foo.txt'));
formData.append('blah', 42);
fetch('http://httpbin.org/post', {
    method: 'POST',
    body: formData
})

结果(仅显示发送的内容):

Result (just showing what is sent):

----------------------------802616704485543852140629
Content-Disposition: form-data; name="file"; filename="foo.txt"
Content-Type: text/plain

hello world!
how do you do?

----------------------------802616704485543852140629
Content-Disposition: form-data; name="blah"

42
----------------------------802616704485543852140629--

这篇关于如何在请求节点获取或节点中发送文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆