Blob下载在IE中不起作用

问题描述 投票:37回答:9

我在我的Angular.js控制器中有下载CSV文件:

 var blob = new Blob([csvContent.join('')], { type: 'text/csv;charset=utf-8'});
 var link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
 link.href = URL.createObjectURL(blob);
 link.download = 'teams.csv';
 link.click();

这适用于Chrome,但不适用于IE。浏览器控制台日志说:

HTML7007:通过关闭为其创建的blob来撤消一个或多个blob URL。这些URL将不再解析,因为已释放支持URL的数据。

它是什么意思,我该如何解决?

javascript internet-explorer angularjs blob compatibility
9个回答
69
投票

尝试使用,thisuseragent

if (navigator.appVersion.toString().indexOf('.NET') > 0)
        window.navigator.msSaveBlob(blob, filename);
else
{
 var blob = new Blob(['stringhere'], { type: 'text/csv;charset=utf-8' });
 var link = document.createElementNS('http://www.w3.org/1999/xhtml', 'a');
 link.href = URL.createObjectURL(blob);
 link.download = 'teams.csv';
 link.click();
}

36
投票

IE不允许你直接打开blob。你必须使用msSaveOrOpenBlob。还有msSaveBlob

if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}

17
投票

我需要使用Blob来下载转换后的base64 PNG图像。我能够用window.navigator.msSaveBlob在IE11上成功下载blob

请参阅以下msdn链接:http://msdn.microsoft.com/en-us/library/hh779016(v=vs.85).aspx

具体来说,你应该打电话:

window.navigator.msSaveBlob(blobObject, 'msSaveBlob_testFile.txt');

其中blobObject是以通常方式创建的Blob。


7
投票

Complete Solution for Chrome, Internet Explorer Firefox and Opera

这个页面上有很多不错的东西,但是我必须结合使用一些东西来完成所有工作。希望这会对你有所帮助。

  1. 使用按钮或链接触发名为download()的函数:
<button class="button-no save-btn" ng-click="download()">DOWNLOAD</button>
  1. 把它放在你的控制器中:
$scope.download = function () {

    // example shows a JSON file
    var content = JSON.stringify($scope.stuffToPutInFile, null, "  ");
    var blob = new Blob([content], {type: 'application/json;charset=utf-8'});

    if (window.navigator && window.navigator.msSaveBlob) {

        // Internet Explorer workaround
        $log.warn("Triggering download using msSaveBlob");
        window.navigator.msSaveBlob(blob, "export.json");

    } else {

        // other browsers
        $log.warn("Triggering download using webkit/
        var url = (window.URL || window.webkitURL).createObjectURL(blob);

        // create invisible element
        var downloadLink = angular.element('<a></a>');
        downloadLink.attr('href', url);
        downloadLink.attr('download', 'export.json');

        // make link invisible and add to the DOM (Firefox)
        downloadLink.attr('style','display:none');
        angular.element(document.body).append(downloadLink);

        // trigger click
        downloadLink[0].click();
    }
};

1
投票

你的IE浏览器版本是什么?你需要一个现代浏览器或IE10 + http://caniuse.com/bloburls


1
投票

也许你需要一些延迟。怎么样:

link.click();
setTimeout(function(){
    document.body.createElementNS('http://www.w3.org/1999/xhtml', 'a');
    URL.revokeObjectURL(link.href);  
}, 100);

1
投票

我需要让下载功能在Chrome和IE11中运行。我在这段代码上取得了很大的成功。

HTML

<div ng-repeat="attachment in attachments">
  <a ng-click="openAttachment(attachment)" ng-href="{{attachment.fileRef}}">{{attachment.filename}}</a>
</div>

JS

$scope.openAttachment = function (attachment) {
  if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(
      b64toBlob(attachment.attachment, attachment.mimeType),
      attachment.filename
    );
  }
};

1
投票

这样做,对我来说很好。

downloadFile(data) {
    if (navigator.msSaveBlob) {
      let blob = new Blob([data], {
        "type": "text/csv;charset=utf8;"
      });
      navigator.msSaveBlob(blob, this.fileName);
    }
    else {
      let blob = new Blob(['\ufeff' + data], { type: 'text/csv;charset=utf-8;' });
      let $link = document.createElement("a");
      let url = URL.createObjectURL(blob);
      $link.setAttribute("target", "_blank");
      $link.setAttribute("href", url);
      $link.setAttribute("download", this.fileName);
      $link.style.visibility = "hidden";
      document.body.appendChild($link);
      $link.click();
      document.body.removeChild($link);
    }
  }

0
投票

尝试使用它:var blob = file.slice(0,file.size);

© www.soinside.com 2019 - 2024. All rights reserved.