Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Edge and opening a blob url [duplicate]

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.

like image 512
Technicolour Avatar asked Aug 26 '15 03:08

Technicolour


People also ask

What does blob in front of URL mean?

The Blob object represents a blob, which is a file-like object of immutable, raw data; they can be read as text or binary data, or converted into a ReadableStream so its methods can be used for processing the data. Blobs can represent data that isn't necessarily in a JavaScript-native format.

How does a blob URL work?

Blob URL/Object URL is a pseudo protocol to allow Blob and File objects to be used as URL source for things like images, download links for binary data and so forth. For example, you can not hand an Image object raw byte-data as it would not know what to do with it.

Can I use URL createObjectURL?

The URL. createObjectURL() static method creates a string containing a URL representing the object given in the parameter. The URL lifetime is tied to the document in the window on which it was created. The new object URL represents the specified File object or Blob object.


1 Answers

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

like image 122
nisiumi Avatar answered Sep 18 '22 05:09

nisiumi