【问题标题】:Open Blob Url in new tab (IE)在新标签页中打开 Blob 网址 (IE)
【发布时间】:2017-09-13 11:17:58
【问题描述】:

我在我的 AngularJS 应用程序中有以下步骤,可以在新选项卡中打开一个文本文件进行保存:

$scope.createString()
                .then(function () {
                    var windowReference = window.open();
                    var blob = new Blob([$scope.createdString], { type: 'text/plain' });
                    var url = (window.URL || window.webkitURL).createObjectURL(blob);
                    windowReference.location = url;
                 });

但这不起作用。它只在新选项卡中打开相同的链接,而不是创建的 Blob-Url。

我做错了什么?我该如何做到这一点?

更新:

我在 IE 中是这样完成的:

$scope.createString()
                .then(function () {
                    var blob = new Blob([$scope.createdString], { type: 'text/plain' });
                    window.navigator.msSaveOrOpenBlob(blob, 'Test.txt');
                 });

但是是否有一种适用于所有浏览器的标准化方式?

【问题讨论】:

标签: javascript html angularjs internet-explorer


【解决方案1】:

我用来完成这项工作的另一种方法是使用传统的 <a> 标签打开 blob,使用 target='_blank' 属性。

获得 blob 对象 URL 后,只需将其插入 href 属性,您将获得如下所示的内容:

<a target="_blank" href="blob:http://example.com/523d3551-80fd-4fb0-a0fc-5194d928cc6d">Print</a>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 1970-01-01
    • 1970-01-01
    • 2011-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多