24

I'm admittedly an AngularJS newbie but I can't find why this code works in Chrome and Firefox but gives "Access is denied" in the javascript console with IE 11.

I need to display a PDF via an authenticated REST call. Ideally this would be displayed in a popup (preview) kind of window.

Code thus far looks like:

$http.post( url, payload, {
    headers : {
        "Authorization": token
    }, 
    responseType: "arraybuffer"
}).success(function ( data ) {
    var file = new Blob( [ data ], { type: 'application/pdf' });
    var fileURL = URL.createObjectURL( file );
    window.open( fileURL );
}

The window.open() gives the "access is denied" message for IE11, but works in Chrome and Firefox. I tried changing to window.location(), and got the same error.

This isn't a cross-domain issue (everything is in the same foo.net domain).

Sampson
  • 265,109
  • 74
  • 539
  • 565
Matt
  • 2,001
  • 2
  • 17
  • 15
  • The easiest way to do this would be instead of returning the file return a url to the file and then you can just open it in a new window. To open a window in Angular use $window (inject it to the controller) https://docs.angularjs.org/api/ng/service/$window – Wayne Ellery Dec 13 '14 at 23:32
  • 1
    Ie uses a different syntax for local files. Use: window.navigator.msSaveBlob(blob, 'file.txt'); or window.navigator.msSaveOrOpenBlob(blob, 'file.txt'); http://msdn.microsoft.com/en-us/library/ie/hh673542(v=vs.85).aspx – Wayne Ellery Dec 13 '14 at 23:36
  • 1
    See http://stackoverflow.com/a/27257511/54680 – Sampson Dec 14 '14 at 01:41

1 Answers1

55

Saving text in a local file in Internet Explorer 10

It looks like IE blocks window.open on blobs, but implemented their own functions for opening and saving blobs. Instead try

if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob);
}
else {
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}
Community
  • 1
  • 1
  • 1
    Worked for me to to solve the issue I posted http://stackoverflow.com/questions/32206672/access-denied-cors-request-in-ie10-in-angularjs-with-required-cross-origin-reso – radder5 Aug 26 '15 at 07:28