1

i am trying to export the records to the excel. Here i have more than 3000 records. When i have below 3000 records it was exporting and saving. But if i have more than 3000 records i am facing the issue. while saving Failed - network error is coming.

Used below code to export the records to excel.

function SaveContents(element, filename) {
if (window.Blob && window.navigator.msSaveOrOpenBlob) {
    var blob = new Blob([element], {
        type: 'data:application/vnd.ms-excel'
    });
    window.navigator.msSaveBlob(blob, filename + '.xls');
} else {
    var a = document.createElement('a');
    document.body.appendChild(a);
    a.href = 'data:application/vnd.ms-excel;charset=utf-8,%EF%BB%BF' + encodeURIComponent(element);
    a.download = filename + ".xls";
    a.click();
}}

Please give a solution to this.

  • Check the value of `blob.size`. Could possibly be the blob size exceeding the max allowed. https://stackoverflow.com/questions/28307789/is-there-any-limitation-on-javascript-max-blob-size – Brett Gregson Sep 10 '19 at 14:45

1 Answers1

0

This is not how you should be doing it any more. Quite a while ago, two things were added:

  • download attribute for <a> that initiates download of the target. Example:

    <a href="myfile.png" download="image.png">Download image</a>
    
  • URL.createObjectURL which converts blob to a virtual URL that can be treated as a file on the internet, try this for example:

    // convert letters to bytes
    const letters = new Uint8Array([..."Hello world"].map((x)=>x.charCodeAt(0)));
    const blob = new Blob([letters], {type:"text/plain"});
    // Try to open the result in new tab
    console.log(URL.createObjectURL(blob));
    // remember to clear the URL later using URL.revokeObjectURL
    

So replace the part of your code that sets the URL like this:

var a = document.createElement('a');
document.body.appendChild(a);
var blob = new Blob([element], {
    type: 'data:application/vnd.ms-excel'
});
a.href = URL.createObjectURL(blob);
a.download = filename + ".xls";
a.click();
// lazy cleanup, note that this renders the link invalid
setTimeout(()=>{URL.revokeObjectURL(a.href);}, 500);
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778