2017-08-14 583 views
0

我創建了一個使用.NET(使用實體框架)和AngularJS檢索PDF文件的簡單Web應用程序。我可以在桌面瀏覽器和iOS瀏覽器上使應用程序完美工作。不幸的是,我在使用Android瀏覽器時遇到問題。Blob URL無法在Android瀏覽器上工作

我的控制器內部是一個功能,通過點擊一個簡單的按鈕來觸發,該按鈕可以請求並顯示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或無法下載Android上的文件。

任何建議,將不勝感激:)。

讓我知道,如果我可以提供任何額外的信息

回答

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)); 
     } 
    }) 
相關問題