0

I am receiving an XmlHttpRequest's response in my code, which is a binary stream of a file. The response has the correct size and mimetype. I would like to download this file to disk, by issuing the typical browser prompt for saving files. I have the following script in place which works perfectly fine in Chrome. It fails for IE though.

var url = /my_rest_url/;
var xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.responseType = "blob";

xhr.onreadystatechange = function (e) {
 if (xhr.readyState == xhr.DONE) {
    var blob = xhr.response;
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
 }
}

xhr.send();

The failure happens on the window.open() call, where IE fails with an exception that says "access is denied". I have researched for hours on end without finding a solution which works for me. I have seen some recommendations to use a 'hidden iframe' to start the download, but it hasn't worked out for me. Has anyone ever made something like this work in IE? Would be glad to get some ideas.

ps/ I know blob and createObjectURL are relatively new. And even though they are supported by IE10 and above, I would really feel better to get rid of these and achieve this an 'old-school' way, if there is any.

shasan
  • 178
  • 2
  • 13
  • Are you running into a Same Origin issue? You should also mention what version of IE you are using or testing against. – Jeremy J Starcher Aug 08 '14 at 23:46
  • @JeremyJStarcher Yes, it is the same-origin policy issue. The object url looks something like 'blob:http%3A//localhost%3A'. But since other browsers allow this I would think this is not a violation of the policy, but rather a limitation in IE. I am testing for IE10. Do you have any ideas? Thanks! – shasan Aug 09 '14 at 03:32
  • Though this talks about canvas, it may be related... http://stackoverflow.com/questions/16956295/ie10-and-cross-origin-resource-sharing-cors-issues-with-image-canvas – Jeremy J Starcher Aug 09 '14 at 04:39

0 Answers0