Blob URL在Android浏览器上无法使用

4

我使用.NET(带Entity Framework)和AngularJS创建了一个简单的Web应用程序来检索PDF文件。我能够使该应用程序在桌面浏览器和iOS浏览器上完美运行。不幸的是,我在任何Android浏览器上都无法使其正常工作。

在我的控制器中,有一个由简单按钮上的ng-click触发的函数,该函数请求并显示pdf。我能够使用这里的解决方案,在iOS和Edge浏览器上正确地使用PDF。

appModule.controller("cellController", ['$scope','$http','$routeParams','$window','$sce', function ($scope, $http, $routeParams,$window,$sce) {
....
$scope.getLayout = function () {
    var attachmentPromise = $http.get("/api/pdf" + $routeParams.ID, { responseType: 'blob' }).then(function (result) {                                    
        var file = new Blob([result.data], { type: 'application/pdf' });
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(file, "Layout.pdf");
        }
        else if (window.navigator.userAgent.match('CriOS')) {
            var reader = new FileReader();
            reader.onloadend = function () { $window.open(reader.result); };
            reader.readAsDataURL(file);
        }
        else if (window.navigator.userAgent.match(/iPad/i) || window.navigator.userAgent.match(/iPhone/i)) {
            var url = $window.URL.createObjectURL(file);
            window.location.href = url;
        }
        else {
            var url = window.URL || window.webkitURL;
            window.open(url.createObjectURL(file));
        }
    });
};
}]);

如上所述,上述代码在桌面和iOS上运行良好,但在Android上会打开一个空白的blob页面或无法下载文件。

如果有任何建议,将不胜感激:)。

如果需要提供任何其他信息,请告知。

1个回答

0

我无法使其按照需要的方式工作。为了解决问题,如果用户使用的是Android设备,我会通过打开文件地址窗口将文档下载到本地存储中。希望这是未来可以解决的问题。

var attachmentPromise = $http.get("/api/" + $routeParams.ID, { responseType: 'blob' }).then(function (result) {                                    
        var file = new Blob([result.data], { type: 'application/pdf' });
        if (window.navigator.msSaveOrOpenBlob) {
            window.navigator.msSaveOrOpenBlob(file, "Layout.pdf");
        }
        else if (window.navigator.userAgent.match(/Chrome/i) && window.navigator.userAgent.match(/Mobile/i)) {
            window.open("/api/" + $routeParams.ID)
        }
        else if (window.navigator.userAgent.match('CriOS')) {
            var reader = new FileReader();
            reader.onloadend = function () { $window.open(reader.result); };
            reader.readAsDataURL(file);
        }
        else if (window.navigator.userAgent.match(/iPad/i) || window.navigator.userAgent.match(/iPhone/i)) {
            var url = $window.URL.createObjectURL(file);
            window.location.href = url;
        }
        else {
            var url = window.URL || window.webkitURL;
            window.open(url.createObjectURL(file));
        }
    }) 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接