我的 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');
});
但是有没有一种适用于所有浏览器的标准化方法?
最佳答案
我用来完成这项工作的另一种方法是使用传统的 <a>
标记来打开 blob,使用 target='_blank'
属性。
获得 Blob 对象 URL 后,只需将其插入 href
属性,您将得到如下所示的内容:
<a target="_blank" href="blob:http://example.com/523d3551-80fd-4fb0-a0fc-5194d928cc6d">Print</a>
关于javascript - 在新选项卡中打开 Blob Url (IE),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43458249/