javascript Windows Edge 并打开一个 blob url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32217537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 14:59:25  来源:igfitidea点击:

Windows Edge and opening a blob url

javascriptmicrosoft-edgebloburls

提问by Technicolour

I'm getting some odd results when trying to open a new window with a blob url in Windows Edge (20.10240.16384, which is the version in the IE11 VM supplied by Microsoft).

尝试在 Windows Edge(20.10240.16384,这是 Microsoft 提供的 IE11 VM 中的版本)中打开一个带有 blob url 的新窗口时,我得到了一些奇怪的结果。

var xhr = new XMLHttpRequest();
xhr.open('POST', sourceUrl, true);
xhr.responseType = 'blob';

xhr.onload = function(e,form) {
    if (this.status == 200) {
        var blob = this.response;
        var url = window.URL.createObjectURL(blob);
        var w = window.open(url);
    }
}

On the line

在线上

var w = window.open(url);

I'm getting an "Access is denied" error which looks to be tied up with CORS ,which makes sense a little as it's not technically the same domain. However a BLOB url doesn't technically have a domain?

我收到一个“访问被拒绝”错误,它看起来与 CORS 相关,这有点道理,因为它在技术上不是同一个域。但是,BLOB url 在技术上没有域?

Is this a bug in Edge? Or am I doing something not quite right? This code works in IE, Chrome etc.

这是Edge中的错误吗?还是我做错了什么?此代码适用于 IE、Chrome 等。

回答by nisiumi

I found out the solution on both IE and Edge.

我在 IE 和 Edge 上都找到了解决方案。

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

The link Here

链接在这里