“Access is Denied” when attempting to open a URL generated for a procedurally-generated PDF in IE11

萝らか妹 提交于 2020-01-13 18:11:08

问题


For an application I am working on, we have a feature where we are generating a report for an object on the server side, and opening it in a new tab (for the time being) on the client.

I'm using the URL.createObjectURL function to make a URL for a Blob, which is comprised of the results of an AJAX call. Whenever a $window.open(generatedFileUrl) call is made, however, I recieve a JavaScript error.

Controller:

(function() {
    angular.module('app').controller('someCtrl', [
        '$window', 'someSvc', controller
    ]);

    function controller($window, someSvc) {
        var vm = this;
        vm.thing = {};  // How we get the object is unimportant for this question.
        vm.printThing = printThing;

        function printThing() {
            someSvc.printThing(vm.thing.id, vm.someFlag)
                .then(function(result) {
                    var file = new Blob([result], {type: 'application/pdf'});
                    var fileURL = URL.createObjectURL(file);

                    $window.open(fileURL);
                });
        }
    }
)();

Service:

(function () {
    angular.module('app').factory('someSvc', [
        '$http', someSvc
    ]);

    function someSvc($http) {
        var service = {
            printThing: function(thingId, someFlag) {
                var args = {
                    'thingId': thingId,
                    'someFlag': someFlag
                };

                return $http.get('/Reports/SomeReport', { 'params': args });
            }
        };

        return service;
    }
})();

The server side code is unimportant to this question.

Question: Why is it that in my controller code, I get the error message,0x80070005 - JavaScript runtime error: Access is denied. in IE11? Additionally, in what way can I avoid the Access Is Denied error?


回答1:


IE won't allow you to open blobs directly. You have to use msSaveOrOpenBlob. There's also msSaveBlob.

if (window.navigator && window.navigator.msSaveOrOpenBlob) {
    window.navigator.msSaveOrOpenBlob(blob, fileName);
} else {
    var objectUrl = URL.createObjectURL(blob);
    window.open(objectUrl);
}


来源:https://stackoverflow.com/questions/36984907/access-is-denied-when-attempting-to-open-a-url-generated-for-a-procedurally-ge

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!