使用javascript |下载base64数据IE11 [英] Download base64 data using javascript | IE11
问题描述
我正在尝试使用JavaScript中的window.location.href下载base64数据。它在Chrome中工作正常,但相同的代码在IE11中无法正常工作。
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?
以下是代码:
Javascript:
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:
HTML:
注意:
我正在开发一个离线网站,我将文件存储在base64字符串格式的浏览器localStorage中,该格式未连接到服务器。我没有任何物理文件。
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.
推荐答案
以下内容适用于所有浏览器
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);
}
这篇关于使用javascript |下载base64数据IE11的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!