Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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();
like image 423
JLYK Avatar asked Oct 16 '13 21:10

JLYK


1 Answers

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();
like image 76
Kinlan Avatar answered Nov 06 '22 18:11

Kinlan