简体   繁体   English

流式写入来自JavaScript的下载文件

[英]Streaming writes to a downloading file from JavaScript

I'm trying to generate very large PDF files in a browser and it is not feasible to do this with very large amounts of data, because browsers end up running out of memory before the document is completed. 我正在尝试在浏览器中生成非常大的PDF文件,并且对大量数据执行此操作是不可行的,因为在文档完成之前,浏览器最终会耗尽内存。 Therefore, I'm looking into a way to stream writes into a file, one page at a time, so that I can free up memory once I finish writing a page. 因此,我正在研究一种将写入操作一次流式传输到文件中的方法,以便在完成写入页面后可以释放内存。

While I obviously don't get file managing capabilities in browsers (obviously), Jimmy Wärting came up with a most interesting solution in StreamSaver.js . 虽然我显然没有得到文件管理的浏览器(显然)功能,吉米Wärting想出了一个最有趣的解决方案StreamSaver.js The main idea here is to use a ServiceWorker to mock an HTTP request with Content-Disposition: Header, which will start a download, and then pipe the data through it. 这里的主要思想是使用ServiceWorker来模拟具有Content-Disposition:Header的HTTP请求,该请求将开始下载,然后通过它传输数据。

The issues with these approach are mainly of compatibility: Service Workers aren't supported in all browsers, nor are Web Streams, and they force us to use HTTPS, which may not be feasible in some scenarios. 这些方法的问题主要是兼容性:并非所有浏览器都支持Service Worker,Web Streams也不受支持,它们迫使我们使用HTTPS,这在某些情况下可能不可行。 So I wonder whether there is an alternative approach that doesn't involve the use cutting-edge features? 因此,我想知道是否存在不涉及使用尖端功能的替代方法?

I don't think you can achieve streamserver kind of things with legacy web capabilities without a hacky way(may be storing the streamed data in IndexDB which has relatively more support , still not all), there is a reason to not choose latest web standards like PWA-service workers. 我不认为您可以通过传统的Web功能来实现流服务器的功能,而又不能轻易实现(可能是将流数据存储在具有相对更多支持的 IndexDB中,但还不是全部),所以有理由不选择最新的Web标准像PWA服务人员一样。 IndexedDB is pretty much part of PWA as well, but the question is what suits as your solution better. IndexedDB也几乎是PWA的一部分,但问题是什么才适合您的解决方案。

Service workers are available in most major browsers now . 现在,大多数主要的浏览器中都可以使用服务人员 If you are still concerned of supporting users in older versions of browser, you can consider using polyfill to support service worker for them. 如果仍然担心在较旧版本的浏览器中支持用户,则可以考虑使用polyfill为他们支持服务工作者。 With less developers concerned of supporting old browsers on such use cases, you might find less documentation/support around such polyfill implementation. 由于较少的开发人员关心在这种用例上支持旧浏览器,因此您可能会发现关于这种polyfill实现的文档/支持更少。

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

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