Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download base64 data using javascript | IE11 [duplicate]

I am trying to download base64 data using “window.location.href” in JavaScript. It works fine in Chrome but the same code is not working in IE11.

Could you please let me know the fix or a workaround for the same?

Here is the code: Javascript:

function DownloadPdf() {
window.location.href = "data:application/pdf;base64,JVBERi0xLjMNJeLjz9MNCj........Pg1zdGFydHhyZWYNMTczDSUlRU9GDQ=="

}

function DownloadExcel() {
window.location.href = "data:application/vnd.ms-excel;base64,UEsDBBQABgAIAAAAIQB......BLBQYAAAAACgAKAIACAACzHAAAAAA="
}

HTML:

NOTE:

I am developing an offline website where I am storing file in browser localStorage in base64 string format which is not connected to server. I don’t have any physical file present.

like image 743
Sunil Avatar asked Apr 29 '26 02:04

Sunil


2 Answers

Below is working for me in all browsers

var blob = new Blob([tdData], { type: 'text/csv' });
if (window.navigator.msSaveBlob) { // // IE hack; see http://msdn.microsoft.com/en-us/library/ie/hh779016.aspx
    window.navigator.msSaveOrOpenBlob(blob, 'exportData' + new Date().toDateString() + '.csv');
}
else {
    var a = window.document.createElement("a");
    a.href = window.URL.createObjectURL(blob, { type: "text/plain" });
    a.download = "exportData" + new Date().toDateString() + ".csv";
    document.body.appendChild(a);
    a.click();  // IE: "Access is denied"; see: https://connect.microsoft.com/IE/feedback/details/797361/ie-10-treats-blob-url-as-cross-origin-and-denies-access
    document.body.removeChild(a);
}
like image 140
Chandra Kudumula Avatar answered May 01 '26 14:05

Chandra Kudumula


I have found a plugin for javascript which may be usefull for you in this case, it is developed to download the base64 content with MIME types specified . Moreover Please have a look at this answer which explains how to download the base64 encoded content .

function fnExport(base64encodedstring) {
    var ua = window.navigator.userAgent;
    var msie = ua.indexOf("MSIE ");
    // If Internet Explorer:
    if (msie > 0 || !!navigator.userAgent.match(/Trident.*rv\:11\./)) {
        txtArea1.document.open("content type", "replace");
        txtArea1.document.write(base64encodedstring);
        txtArea1.document.close();
        txtArea1.focus();
        sa = txtArea1.document.execCommand("SaveAs", true, reportname + ".extension");
        console.log(sa);
    }
    else { //other browser not tested on IE 11
        sa = window.open('data:content-type;base64,' +base64encodedstring);
    }
    return (sa);
}
like image 44
Technacron Avatar answered May 01 '26 14:05

Technacron



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!