gpt4 book ai didi

javascript - Blob URL 不适用于 Android 浏览器

转载 作者:行者123 更新时间:2023-12-04 14:00:55 28 4
gpt4 key购买 nike

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

在我的 Controller 内部是一个由 ng-click 触发的函数,该函数请求并显示 pdf 的简单按钮。我能够使用解决方案 here让 PDF 在 iOS 和 Edge 浏览器上正常工作。

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 页面,要么无法在 Android 上下载文件。

对此的任何建议将不胜感激:)。

让我知道我是否可以提供任何其他信息

最佳答案

我无法让它按需要工作。作为一种解决方法,如果用户使用的是 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));
}
})

关于javascript - Blob URL 不适用于 Android 浏览器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45682315/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com