从IE 11中的HTTP URL下载Blob [英] Download a blob from HTTP URL in IE 11

查看:327
本文介绍了从IE 11中的HTTP URL下载Blob的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的页面生成如下URL:blob:http%3A//localhost%3A8383/568233a1-8b13-48b3-84d5-cca045ae384f,包含文件数据的blob.我将在IE 11以外的所有浏览器中将其下载为文件.如何在IE 11中下载此Blob?一个新的标签页打开并持续刷新.

My page generates a URL like this: blob:http%3A//localhost%3A8383/568233a1-8b13-48b3-84d5-cca045ae384f, blob having file data. I am downloading this as a file in every browser except IE 11. How can I download this blob in IE 11? A new tab get open and continuous refreshing happen.

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();
}

了解更多:固定的URL.createObjectURL()函数在IE 11中不起作用

演示: JSFiddle

这篇关于从IE 11中的HTTP URL下载Blob的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆