IE + XMLHttp + CreateObjectURL Error

2019-08-11 20:10发布

问题:

I am trying to download and show the contents of a remote file inside an iFrame , and succeeded in all browsers except for IE(i am trying with IE 10). I have used XMLHttpRequest,Blob,CreateOBjectUrl APIs to complete the process.

In IE i am not able to view the file content inside the iFrame and also no particular error messages appeared on console as well.

I had pasted my code at the bottom of this thread , and a step by step explanation as below

  1. Getting the download document url & corresponding mime type(Perfectly fine in all broswers).
  2. Invoking XMLHttp Request , a Http GET Async call ,as response type as 'arraybuffer' (Perfectly fine in all browsers) Upon completing the XMLHttpGet below 3 steps are executing.
  3. Creating a blob using the proper mimetype ;(Perfectly fine in all other browsers, specially verified the blob by downloading it in IE using MSSaveOrOpenBlob method). 4.InOrder to bind the blob contents to the iFrame , create the blob url using "createObjectURL" (Perfectly fine in all browsers , but in IE we are not getting a perfect URL).
  4. Finally binding the URL with the iFrame for display.

Code snippet below.

// Getting the document url and mime type ( Which is perfectly fine )

  var downloadUrl=finalServerURL + "DocumentService.svc/GetItemBinary?id=" + itemId + "&version=" + version;

var mimeTypeForDownload = responseStore.mimeTypes[currentlySelectedObject.fileExtension];



  window.URL = window.URL || window.webkitURL;

//Defining the XML Http Process

                var xhr = new XMLHttpRequest();

                xhr.open('GET', downloadUrl, true);

                xhr.responseType = 'arraybuffer'; //Reading as array buffer .

                xhr.onload = function (e) {

                    var mimeType = mimeTypeForDownload;

                    var blob = new Blob([xhr.response], { type: mimeType });

                    // Perfect blob, we are able to download it in both IE and non-IE browsers



                    //This below url  from createObjectURL,

                    //Working perfectly fine in all non-IE browsers, but nothing happening in IE

                    var url = window.URL.createObjectURL(blob);



                document.getElementById(documentContentiFrameId).setAttribute("src", url);



                };

                xhr.send;

Please let me if you get any information on this , would be really helpful.

回答1:

I came to know that its not possible in IE to get a proper URL for your blob entries , none of my attempts are get succeeded. My alternative solutions, 1)go for pdf.js , an open source javascript library , which allows to render pdf binaries and equivalent pdf blobs. 2)Write your own viewers by utilizing the open PDF libraries , which will be time consuming , and more learning efforts involved.

Thanks, Vishnu