[英]Can I use javascript to locally create and save a humongous file?
I'd like to use javascript to create and save a large .csv file client-side. 我想用javascript创建并保存一个大的.csv文件客户端。 This has been asked and answered before (see 1 , 2 , 3 , or 4 , for example), and this comes down to two approaches (among others):
这已经被问和回答之前(见1 , 2 , 3或4 ,例如),这可以归结为两种方法(其中包括):
Use FileSaver.js to implement w3c's saveAs()
: 使用FileSaver.js实现w3c的
saveAs()
:
var lines = ["line 1,cell 11,cell 12\\n","line 2,cell 21,cell 22\\n"]; var type = "text/csv;charset=utf-8"; var blob = new Blob(lines,{"type":type}); saveAs(blob,"download.csv");
Use a[download]
and a data uri: 使用
a[download]
和数据uri:
var lines = ["line 1,cell 11,cell 12\\n","line 2,cell 21,cell 22\\n"]; var type = "text/csv;charset=utf-8"; var downloader = $('<a download href="data:'+type+','+escape(lines.join(''))+'"></a>') .appendTo("body") downloader[0].click(); downloader.remove();
My problem is that my file can be gigantic: 18 million lines and 2.5 Gb (and I'd like to be able to handle more). 我的问题是我的文件可能是巨大的:1800万行和2.5 Gb(我希望能够处理更多)。 Creating
lines
uses too much memory and crashes the site. 创建
lines
会占用太多内存并导致网站崩溃。 But there's no reason to store lines
in the browser's memory just to save it to the hard drive. 但是没有理由在浏览器的内存中存储
lines
只是为了将它保存到硬盘驱动器中。 Is there a way to create a progressive download using javascript (in other words, start the download but keep appending lines as I calculate them)? 有没有办法使用javascript创建渐进式下载(换句话说,开始下载,但在我计算时保持追加行)? Or would my only option be to download the file as separate chunks that the user must then join together?
或者我唯一的选择是将文件作为单独的块下载,然后用户必须将它们连接在一起?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.