从IE 11中的HTTP URL下载blob

时间:2016-05-02 09:08:16

标签: javascript jquery angularjs blob

我的网页会生成如下网址: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();

3 个答案:

答案 0 :(得分:9)

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

答案 1 :(得分:4)

在将IE特定部分改为此后,Fidel90的答案在IE 11中运行良好:

(!window.navigator.msSaveBlob ? false : function (blobData, fileName) {
      return window.navigator.msSaveBlob(blobData, fileName);
})

答案 2 :(得分:2)

在IE中尝试window.navigator.saveBlob(fileURL,name);

有关详细信息,请查看documentation at MSDN

在过去,我创建了以下非常方便的polyfill来检查IE,否则使用href下载。也许它会帮助你(或其他人):

//check for native saveAs function
    window.saveAs = window.saveAs || window.webkitSaveAs || window.mozSaveAs || window.msSaveAs ||
        //(msIE) save Blob API
        (!window.navigator.saveBlob ? false : function (blobData, fileName) {
            return window.navigator.saveBlob(blobData,fileName);
        }) ||
        //save blob via a href and download
        (!window.URL ? false : function (blobData, fileName) {
            //create blobURL
            var blobURL = window.URL.createObjectURL(blobData),
                deleteBlobURL = function () {
                    setTimeout(function () {
                        //delay deleting, otherwise firefox wont download anything
                        window.URL.revokeObjectURL(blobURL);
                    }, 250);
                };

            //test for download link support
            if ("download" in document.createElement("a")) {
                //create anchor
                var a = document.createElement("a");
                //set attributes
                a.setAttribute("href", blobURL);
                a.setAttribute("download", fileName);
                //create click event
                a.onclick = deleteBlobURL;

                //append, trigger click event to simulate download, remove
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            }
            else {
                //fallback, open resource in new tab
                window.open(blobURL, "_blank", "");
                deleteBlobURL();
            }
        });

然后,您可以在应用中的任何位置使用此功能,只需:

window.saveAs(blobData, fileName);