最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Chrome cannot export to csv if there are too many rows? - Stack Overflow

programmeradmin4浏览0评论

I wrote this export button that basically spits out all the data I have on the google table into a CSV for download. It works perfectly fine until I have way too many rows and Chrome gives me the "aw snap" error page when I try to download the csv. How do I fix this?

    var csvContent = "data:text/csv;charset=utf-8,";

    data.forEach(function (infoArray, index) {
        dataString = infoArray.join(",");
        csvContent += dataString + "\n";
    });

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "Data.csv");
    link.click();

I wrote this export button that basically spits out all the data I have on the google table into a CSV for download. It works perfectly fine until I have way too many rows and Chrome gives me the "aw snap" error page when I try to download the csv. How do I fix this?

    var csvContent = "data:text/csv;charset=utf-8,";

    data.forEach(function (infoArray, index) {
        dataString = infoArray.join(",");
        csvContent += dataString + "\n";
    });

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "Data.csv");
    link.click();
Share Improve this question asked Oct 16, 2013 at 21:20 JLYKJLYK 3919 silver badges22 bronze badges 1
  • how many rows are you putting in data? – zzxx53 Commented Oct 16, 2013 at 21:30
Add a ment  | 

1 Answer 1

Reset to default 18

Chrome can only handle HREF's that are roughly 2 million characters long (or less).

You want to add the output to a Blob and then on that blob create an ObjectURL using URL.createObjectURL (MDN) and attach that to the href attribute of the anchor.

An example might be:

var csvContent = "";

data.forEach(function (infoArray, index) {
    dataString = infoArray.join(",");
    csvContent += dataString + "\n";
});

var blobdata = new Blob([csvContent],{type : 'text/csv'});
var link = document.createElement("a");
link.setAttribute("href", window.URL.createObjectURL(blobdata));
link.setAttribute("download", "Data.csv");
document.body.appendChild(link);
link.click();
发布评论

评论列表(0)

  1. 暂无评论