Windows Edge and opening a blob url

Technicolour picture Technicolour · Aug 26, 2015 · Viewed 16.9k times · Source

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).

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?

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

Answer

nisiumi picture nisiumi · May 26, 2017

I found out the solution on both IE and Edge.

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

The link Here