从IE 11中的HTTP URL下载blob

Jef*_*ean 8 javascript jquery blob angularjs

我的页面生成如下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();
Run Code Online (Sandbox Code Playgroud)

bam*_*sza 11

IE11不支持URL.createObjectURL()

为我工作.

IE11我用的

window.navigator.msSaveOrOpenBlob(blob, fileName);
Run Code Online (Sandbox Code Playgroud)

或者,如果检查条件.

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();
}
Run Code Online (Sandbox Code Playgroud)

阅读更多:修复了URL.createObjectURL()函数在IE 11中不起作用

演示:JSFiddle


Fid*_*l90 2

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

有关更多信息,请参阅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();
            }
        });
Run Code Online (Sandbox Code Playgroud)

然后,您可以在应用程序中的任何位置使用它,就像这样简单:

window.saveAs(blobData, fileName);
Run Code Online (Sandbox Code Playgroud)