1

I'm attempting to download a CSV file using the href method, however it appears data is truncated when setting it to a href tag. For IE I used msSaveBlob and that appears to be working correctly and all data is correctly downloaded.

            if (navigator.msSaveBlob) { // IE 10+
                navigator.msSaveBlob(new Blob([data], { type: 'text/csv;charset=utf-8;' }), filename);
            }
            //
            var csvContent = "data:text/csv;charset=utf-8," + data;
            var encodedUri = encodeURI(csvContent);
            var link = document.createElement("a");
            link.setAttribute("href", encodedUri);
            //
            link.setAttribute("download", filename);
            link.innerHTML = "CSV Link - Placeholder";
            document.body.appendChild(link); // Required for FF

            link.click();

These are relatively large files, 9k lines in excel (around 500kb). Any ideas what I can do to stop this truncation? Should I use a different method? Thanks!

Bercik
  • 139
  • 1
  • 14

1 Answers1

3

Solved using below answer: download file using an ajax request

Essentially:

            var file = new Blob([data], {type: 'text/csv;charset=utf-8;'});
            if (window.navigator.msSaveOrOpenBlob) // IE10+
                window.navigator.msSaveOrOpenBlob(file, filename);
            else { // Others
                var a = document.createElement("a"),
                url = URL.createObjectURL(file);
                a.href = url;
                a.download = filename;
                document.body.appendChild(a);
                a.click();
                setTimeout(function() {
                    document.body.removeChild(a);
                    window.URL.revokeObjectURL(url);  
                }, 0); 
            }
Bercik
  • 139
  • 1
  • 14