26

我正在尝试将fetchReadableStream一起使用。在这个例子中,ReadableStream 应该简单地重复“一些数据...”。

fetch('/', {
  method: 'POST', 
  body: new ReadableStream({
    pull: function(controller) {
      console.log('pull called!');
      controller.enqueue('Some data...');
    }
  })
});

这行不通。Whilepull执行一次,请求正文中不会发送任何数据。

POST / HTTP/1.1
Host: example.com
Connection: keep-alive
Content-Length: 0
Origin: https://example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36
Accept: */*
Referer: https://example.com/
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.8

如何使 ReadableStream(或任何可以写入动态数据的流)可用于 fetch?

或者,如果这还不可能,您能否指出这一点?谢谢你。

注意:这是一个更具体的衍生问题: Method for streaming data from browser to server via HTTP

4

1 回答 1

18

我们正在努力完成这项工作,请参阅https://github.com/whatwg/fetch/pull/425了解 Fetch Standard 的 PR。完成后,您可以期望它(慢慢地)进入浏览器。

于 2016-12-19T12:14:40.300 回答