Setting window.location or window.open in AngularJS gives "access is denied" in IE 11
Asked Answered
S

1

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

Summersummerhouse answered 13/12, 2014 at 21:40 Comment(3)
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) docs.angularjs.org/api/ng/service/$windowCalvaria
Ie uses a different syntax for local files. Use: window.navigator.msSaveBlob(blob, 'file.txt'); or window.navigator.msSaveOrOpenBlob(blob, 'file.txt'); msdn.microsoft.com/en-us/library/ie/hh673542(v=vs.85).aspxCalvaria
See https://mcmap.net/q/582139/-downloading-a-dynamic-csv-in-internet-explorerDemb
K
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);
}
Kellogg answered 18/3, 2015 at 15:11 Comment(1)
Worked for me to to solve the issue I posted #32207172Chilli

© 2022 - 2024 — McMap. All rights reserved.