通过 HTTP 从浏览器向服务器流式传输数据的方法 [英] Method for streaming data from browser to server via HTTP

查看:135
本文介绍了通过 HTTP 从浏览器向服务器流式传输数据的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何类似 XHR 的浏览器 API 可用于通过 HTTP 将二进制文件流式传输到服务器?

Are there any XHR-like browser APIs available for streaming binary to a server over HTTP?

我想随着时间的推移发出 HTTP PUT 请求并以编程方式创建数据.我不想一次创建所有这些数据,因为它可能存在于内存中.一些伪代码来说明我的意思:

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);
});

我目前有一个网络套接字解决方案,但更喜欢常规 HTTP 以便与一些现有的服务器端代码更好地互操作.

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,因为它支持 ArrayBuffers、DataViews、Files 等请求主体.如果我能以某种方式伪造这些对象之一,以便我可以将 Fetch API 与动态数据一起使用,那对我来说就行了.我尝试创建一个 Proxy 对象,看看是否有任何方法被调用,我可以修补.不幸的是,浏览器(至少在 Chrome 中)似乎正在读取本机代码而不是 JS 领域.但是,如果我错了,请纠正我.

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 进行上游).我目前发现的,看起来好像我们正在搜索浏览器功能设计的前沿;-)

XMLHttpRequest 定义 在第 4 步中说明 bodyinit 表示此内容提取是(或可以是)可读流.

我仍在搜索(作为非网络开发人员)有关如何创建这样的东西并将数据馈送到可读流"的另一端"(即应该是可写流",但我还没有没找到).

也许你更擅长搜索,如果你找到了实现这些设计计划的方法,可以在这里发布.

^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

这篇关于通过 HTTP 从浏览器向服务器流式传输数据的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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