简体   繁体   English

我可以使用javascript在本地创建并保存一个庞大的文件吗?

[英]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): 这已经被问和回答之前(见1234 ,例如),这可以归结为两种方法(其中包括):

  1. 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"); 
  2. 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? 或者我唯一的选择是将文件作为单独的块下载,然后用户必须将它们连接在一起?

If you are using HTML5, you can try using the FileSytem API. 如果您使用的是HTML5,则可以尝试使用FileSytem API。 See here for information on the subject, and here for a more practical example on how to create and access files fron the client-side. 有关主题的信息,请参阅此处此处有关如何在客户端创建和访问文件的更实用示例。

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

相关问题 如何在本地保存javascript变量? - How can I save javascript variables locally? 我如何*本地*保存由javascript生成的.html文件(在* local * .html页面上运行)? - How can I *locally* save an .html file generated by javascript (running on a *local* .html page)? 如何使用JSP创建动态Javascript文件? - How can I use a JSP to create a dynamic Javascript file? 如何配置apache使用php创建一个javascript文件 - How can I configure apache to use php to create a javascript file 如何在Chrome中本地编辑JavaScript文件 - How can I made edit to JavaScript file locally in Chrome 使用javascript在我想要的文件夹中本地保存pdf - Locally save a pdf in the folder I want with javascript 我可以使用Javascript将文件保存到Firefox和Safari中的用户文件系统中吗? - Can I use Javascript to save a file to the user's file system in Firefox and Safari? 我可以在Parse javascript中保存任何文件吗? - Can I save any file in Parse javascript? 如何使用javascript中的Blob创建文件并将其保存在本地系统中? - How can i create a file and save it in local system using Blob in javascript? 如何生成xml文件并允许用户使用javascript将其保存在本地? - how do I generate an xml file and allow the user to save it locally using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM