是否可以动态生成大文件(10Gb)并将其流式传输到客户端? [英] Is it possible to dynamically produce large files (10Gb) and stream them to the client?

查看:113
本文介绍了是否可以动态生成大文件(10Gb)并将其流式传输到客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以动态生成大型文件(10Gb +)供客户端下载?

Is it possible to dynamically produce large files (10Gb+) for the client to download?

我正在构建一个动态创建文件并将其下载到客户。我通过创建一个Blob数据并使用objectUrl来下载它们来实现这一点。

I'm building a webapp that dynamically creates files and downloads them to the client. I've implemented this by creating a Blob of data and using an objectUrl to download them.

(例如:下载大文件在达特朗):

import 'dart:html';

void main() {
  var data = new Blob(['Hello World!\n'*1000000]);
  querySelector("#downloadLink")
      ..setAttribute('href', Url.createObjectUrl(data));
}

但是,这不适用于大文件。
有没有办法将文件传输到客户端,或者将数据附加到文件中,以便在下载之前可以逐个生成文件而不是生成整个文件?

However, this does not work for large files. Is there a way to stream files to the client, or append data to a file, so that the file can be generated piece by piece rather than generating the entire thing before downloading?

推荐答案

for< 800MB我建议 FileSaver 但是对于10GB +,你需要像 StreamSaver (它只适用于Blink doe)16GB +对我没有任何问题

for < 800MB i would recommend FileSaver but for 10GB+ you are going to need something like StreamSaver (It will only work in Blink doe) 16GB+ haven't been any problem for me

const fileStream = streamSaver.createWriteStream('filename.txt')
const writer = fileStream.getWriter()
const encoder = new TextEncoder()

// When you have any data you write it as Uint8array
let data = 'a'.repeat(1024)
let uint8array = encoder.encode(data + "\n\n")

writer.write(uint8array) // chunk
writer.write(uint8array) // chunk
writer.write(uint8array) // chunk
writer.write(uint8array) // chunk

// After you have written all bytes you need to close it
writer.close()

(只是不要一次写完所有块) ,随着你获得更多数据,逐步进行)

(just don't write all chunks at once, do it progressively as you get more data avalible)

这篇关于是否可以动态生成大文件(10Gb)并将其流式传输到客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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