6

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();
1
  • how many rows are you putting in data?
    – zzxx53
    Oct 16, 2013 at 21:30

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.