問題描述
是否有任何類似 XHR 的瀏覽器 API 可用于通過 HTTP 將二進(jìn)制流式傳輸?shù)椒?wù)器?
Are there any XHR-like browser APIs available for streaming binary to a server over HTTP?
我想隨著時(shí)間的推移發(fā)出 HTTP PUT 請求并以編程方式創(chuàng)建數(shù)據(jù).我不想一次創(chuàng)建所有這些數(shù)據(jù),因?yàn)閮?nèi)存中可能存在大量數(shù)據(jù).一些偽代碼來說明我的意思:
I want to make an HTTP PUT request and create data programmatically, over time. I don't want to create all this data at once, since there could be gigs of it sitting in memory. Some psueudo-code to illustrate what I'm getting at:
var dataGenerator = new DataGenerator(); // Generates 8KB UInt8Array every second
var streamToWriteTo;
http.put('/example', function (requestStream) {
streamToWriteTo = requestStream;
});
dataGenerator.on('data', function (chunk) {
if (!streamToWriteTo) {
return;
}
streamToWriteTo.write(chunk);
});
我目前有一個(gè) Web 套接字解決方案,但更喜歡常規(guī) HTTP,以便與一些現(xiàn)有的服務(wù)器端代碼更好地互操作.
I currently have a web socket solution in place instead, but would prefer regular HTTP for better interop with some existing server-side code.
我可以使用最前沿的瀏覽器 API.我正在查看 Fetch API,因?yàn)樗С终埱笳牡?ArrayBuffers、DataViews、Files 等.如果我能以某種方式偽造這些對象之一,以便我可以將 Fetch API 與動(dòng)態(tài)數(shù)據(jù)一起使用,那將適用于我.我嘗試創(chuàng)建一個(gè)代理對象,看看是否調(diào)用了我可以修補(bǔ)的方法.不幸的是,瀏覽器(至少在 Chrome 中)似乎正在閱讀本機(jī)代碼,而不是在 JS 領(lǐng)域.但是,如果我錯(cuò)了,請糾正我.
I can use bleeding edge browser APIs. I was looking at the Fetch API, as it supports ArrayBuffers, DataViews, Files, and such for request bodies. If I could somehow fake out one of these objects so that I could use the Fetch API with dynamic data, that would work for me. I tried creating a Proxy object to see if any methods were called that I could monkey patch. Unfortunately, it seems that the browser (at least in Chrome) is doing the reading in native code and not in JS land. But, please correct me if I'm wrong on that.
推薦答案
我目前正在尋找完全相同的東西(通過 Ajax 上傳).我目前的發(fā)現(xiàn),看起來好像我們正在搜索瀏覽器功能設(shè)計(jì)的最前沿;-)
XMLHttpRequest 定義 在步驟 4 bodyinit 這個(gè)內(nèi)容提取是(或可以是)一個(gè)可讀流.
我仍在(作為非網(wǎng)絡(luò)開發(fā)人員)搜索有關(guān)如何創(chuàng)建這樣的東西并將數(shù)據(jù)提供給可讀流"的另一端"的信息(即應(yīng)該是可寫流",但我還沒有沒找到).
如果您找到了實(shí)施這些設(shè)計(jì)計(jì)劃的方法,也許您在搜索方面做得更好,并且可以在此處發(fā)布.
^5
斯文
I'm currently searching for exactly the same thing (upstreaming via Ajax).
What I currently found, looks as if we are searching at the bleeding edge of browser's feature design ;-)
XMLHttpRequest definition tells in step 4 bodyinit that the content extraction of this is (or can be) a readablestream.
I'm still searching (as a non-webdeveloper) for information of how to create such a thing and to feed data into the "other end" of that "readablestream" (which namely should be a "writablestream", but I yet did not find that).
Maybe you are better in searching and can post here if you found a method to implement these design plans.
^5
sven
這篇關(guān)于通過 HTTP 將數(shù)據(jù)從瀏覽器流式傳輸?shù)椒?wù)器的方法的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!