我的页面生成这样的 URL:blob:http%3A//localhost%3A8383/568233a1-8b13-48b3-84d5-cca045ae384f
,blob 包含文件数据。我正在除 IE 11 之外的所有浏览器中将其作为文件下载。如何在 IE 11 中下载此 blob?将打开一个新选项卡并持续刷新。
var file = new Blob([data], { type: 'application/octet-stream' });
var reader = new FileReader();
reader.onload = function (e) {
var text = reader.result;
}
reader.readAsArrayBuffer(file);
var fileURL = URL.createObjectURL(file);
var filename = fileURL.replace(/^.*[\\\/]/, '');
var name = filename + '.doc';
var a = $("<a style='display: none;'/>");
a.attr("href", fileURL);
a.attr("download", name);
$("body").append(a);
a[0].click();
a.remove();
最佳答案
IE11 不支持 URL.createObjectURL()
为我工作。
我在用IE11
window.navigator.msSaveOrOpenBlob(blob, fileName);
或者,如果检查条件。
var blob = 'Blob Data';
if(window.navigator.msSaveOrOpenBlob) {
// IE11
window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
// Google chome, Firefox, ....
var url = (window.URL || window.webkitURL).createObjectURL(blob);
$('#filedownload').attr('download', fileName);
$('#filedownload').attr('href', url);
$('#filedownload')[0].click();
}
阅读更多:Fixed URL.createObjectURL() function doesn't work in IE 11
演示:JSFiddle
关于javascript - 在 IE 11 中从 HTTP URL 下载 blob,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36978795/