我正在尝试将fetch与ReadableStream一起使用。在这个例子中,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