Javascript:导出大文本/ csv文件导致Google Chrome崩溃 [英] Javascript: Exporting large text/csv file crashes Google Chrome
问题描述
我有以下Javascript代码在客户端导出CSV文件。但是,每当我尝试导出大型数组时,Chrome浏览器崩溃。 Chrome中允许的数据字符串的限制是什么?是否有可能达到Chrome允许的内存限制?如果Chrome的数据字符串太长,我将如何在客户端导出大型CSV文件?
I have the following Javascript code to export CSV file on the client side. However Google Chrome crashes every time I try to export a large array. What is the limit of the data string allowed in Chrome? Is it possible that it is hitting the memory limit allowed in Chrome? If the data string is too long for Chrome, how will I go about exporting large CSV files on the client side?
var csvRows = [...]; //Array with 40000 items, each item is 100 characters long.
var csvString = csvRows.join("\r\n");
var a = document.createElement('a');
a.href = 'data:text/csv;charset=utf-8,' + encodeURIComponent(csvString);
a.target = '_blank';
a.download = 'export.csv';
document.body.appendChild(a);
a.click();
(预计文件大小约为6.4MB)
(Expected file size is about 6.4MB)
推荐答案
有相同的问题,并使用 Blob 。
had the same Problem and solved it using Blobs.
例如:
csvData = new Blob([csvString], { type: 'text/csv' });
var csvUrl = URL.createObjectURL(csvData);
a.href = csvUrl;
资料来源:
http://stackoverflow.com/a/24611096/3048937
这篇关于Javascript:导出大文本/ csv文件导致Google Chrome崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!