Chrome cannot export to csv if there are too many lines?

I wrote this export button, which basically spits out all the data that I have in the google table in CSV for download. It works fine as long as I have too many lines, and Chrome gives me the "aw snap" error page when I try to load csv. How to fix it?

    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();
+4
source share
1 answer

Chrome can only handle HREF with a length of about 2 million characters (or less).

You want to add output to Blob , and then create ObjectURL on this blob using URL.createObjectURL( MDN ) and attach this to the href attribute of the binding.

An example could 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();
+16

All Articles