我使用.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页面或无法下载文件。
如果有任何建议,将不胜感激:)。
如果需要提供任何其他信息,请告知。