【问题标题】:"Access is Denied" when attempting to open a URL generated for a procedurally-generated PDF in IE11尝试在 IE11 中打开为程序生成的 PDF 生成的 URL 时“访问被拒绝”
【发布时间】:2016-08-27 08:43:54
【问题描述】:

对于我正在开发的应用程序,我们有一个功能,我们在服务器端为对象生成报告,并在客户端的新选项卡中(暂时)打开它。

我正在使用URL.createObjectURL 函数为Blob 创建一个URL,该URL 由AJAX 调用的结果组成。但是,每当调用 $window.open(generatedFileUrl) 时,我都会收到 JavaScript 错误。

控制器:

(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);
                });
        }
    }
)();

服务:

(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;
    }
})();

服务器端代码对这个问题并不重要。

问题:为什么在我的控制器代码中,我在 IE11 中收到错误消息0x80070005 - JavaScript runtime error: Access is denied.?另外,我可以通过什么方式避免 Access Is Denied 错误?

【问题讨论】:

    标签: javascript angularjs pdf-generation


    【解决方案1】:

    IE 不允许您直接打开 blob。你必须使用msSaveOrOpenBlob。还有msSaveBlob

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

    【讨论】:

    • 我的错,我在想msSaveBlob。确实存在。
    猜你喜欢
    • 2022-08-18
    • 2022-10-07
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 2020-12-13
    • 2021-02-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多