2016-12-02 22 views
4

我試圖使用fetchReadableStream。在這個例子中,ReadableStream應該無限地重複「一些數據...」。使用ReadableStream獲取

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

這是行不通的。雖然pull只執行一次,但沒有數據在請求主體中發送。

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(或任何一種可以寫動態數據的流)可用於獲取?

或者,如果這還不可能,請您註明這一點?謝謝。

注:這是一個更具體的分拆問題:Method for streaming data from browser to server via HTTP

+1

https://github.com/whatwg/fetch/issues/439 – guest271314

回答

3

我們正在使這項工作,請參閱https://github.com/whatwg/fetch/pull/425爲PR的提取標準。一旦完成,您可以預期這會進入瀏覽器(緩慢)。

+0

謝謝。 MDN文檔今天說什麼說ReadableStream是可用的?今天是什麼使用它? – Brad

+0

它可用於Chrome中的Response對象。 – Anne

+1

然後可能只是一個文檔怪癖,但MDN文檔說可以在請求中使用ReadableStream。 https://developer.mozilla.org/en-US/docs/Web/API/Request – Brad