【问题标题】:Download a blob from HTTP URL in IE 11从 IE 11 中的 HTTP URL 下载 blob
【发布时间】:2016-05-02 09:08:15
【问题描述】:

我的页面生成这样的 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();

【问题讨论】:

  • stackoverflow.com/questions/36792681/… 如果有帮助,请查看我的这个答案
  • @Rakeschand,我已经使用 window.navigator.msSaveOrOpenBlob(blob, "Name your file here"),但我不希望像在 IE 11 中一样打开、保存或另存为选项,我需要与其他浏览器相同的行为,即在 IE 11 中直接下载文件,是否有任何可能的方式在 IE 11 中直接下载文件而无需任何提示,location.href = url,在其他浏览器中有效,但在 IE 11 中表示权限拒绝
  • 但该解决方案适用于我,无需任何打开、保存或另存为的提示。
  • @JeffDean:我的解决方案对您有帮助吗?
  • @Rakeschand 你的浏览器不能是 IE 11,我认为你在 IE

标签: javascript jquery angularjs blob


【解决方案1】:

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

【讨论】:

  • 你拯救了我的一天。谢谢。
【解决方案2】:

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

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

【讨论】:

    【解决方案3】:

    在 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);
    

    【讨论】:

    • 我已经完成了您提供的代码,但是在 IE 11 中,当我使用时,出现以下错误,0x80070005 - JavaScript 运行时错误:访问被拒绝。对于代码window.open(blobURL, "_blank", "");,我的要求是绕过提示打开,保存或另存为,以便我可以直接下载它
    • 如果您的浏览器提示您可能取决于您的浏览器设置。
    • 是的,如果浏览器IE
    猜你喜欢
    • 2020-03-29
    • 2021-10-29
    • 2017-12-21
    • 1970-01-01
    • 1970-01-01
    • 2017-01-23
    • 1970-01-01
    • 2019-09-27
    • 2017-12-23
    相关资源
    最近更新 更多