简体   繁体   English

在客户端生成可下载的文件流

[英]Generate downloadable file stream on client-side

I use GWT, but this is probably generally applicable to JavaScript.我使用 GWT,但这可能通常适用于 JavaScript。

I download a low single-digit number of megabytes of raw data from the server.我从服务器下载了低个位数兆字节的原始数据。 I want to generate a downloadable CSV file from that raw data, but I want to do it on the client-side because the CSV will be huge compared to the raw data and transmitting all of that over the wire would be stupid.我想从该原始数据生成一个可下载的 CSV 文件,但我想在客户端执行此操作,因为与原始数据相比,CSV 将是巨大的,并且通过网络传输所有这些将是愚蠢的。

Generating the entire file content at once and then prompting the user for a download seems to be possible, but concatenating hundrets of megabytes of strings into a URL would be stupid.一次生成整个文件内容然后提示用户下载似乎是可能的,但是将成百上千的兆字节字符串连接到一个 URL 中是愚蠢的。

In any sane language I would read from the response stream into a buffer, process that buffer, generate a chunk of CSV text and write that to the file stream.在任何理智的语言中,我都会将响应流读入缓冲区,处理该缓冲区,生成一大块 CSV 文本并将其写入文件流。 Repeat until all of the raw data is processed.重复直到处理完所有原始数据。

Is something like this possible in JavaScript?在 JavaScript 中可能有这样的事情吗?
Does GWT have a predefined mechanism for this? GWT 对此有预定义的机制吗?

Ideally there should also be a way of showing progress.理想情况下,还应该有一种显示进度的方式。

You can use the browser's Fetch API.您可以使用浏览器的 Fetch API。

When you call fetch it will return a Response object.当您调用fetch ,它将返回一个Response对象。

The Response.body getter returns a ReadableStream object which is the response stream you are wanting. Response.body getter 返回一个ReadableStream对象,它是您想要的响应流。

The browser API is accessible in GWT using the GWT elemental2 library.可以使用 GWT elemental2库在 GWT 中访问浏览器 API。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM