2014-02-12 41 views
25

過去幾天我一直致力於this problem。嘗試在<embed src>標記上顯示流時沒有運氣,我只是試圖在新窗口中顯示它。PDF Blob - 彈出窗口未顯示內容

新窗口顯示PDF 僅控制enter image description here

任何想法,爲什麼PDF的內容沒有顯示?

CODE:

$http.post('/fetchBlobURL',{myParams}).success(function (data) { 
    var file = new Blob([data], {type: 'application/pdf'}); 
    var fileURL = URL.createObjectURL(file); 
    window.open(fileURL); 
}); 
+0

使用'$ window'(帶有美元符號)。 – naXa

+0

看到我的問題和答案在這裏:http://stackoverflow.com/questions/41390623/post-complex-data-and-open-generated-pdf-in-new-window – Groppe

回答

69

您需要的responseType設置爲arraybuffer,如果你想從你的迴應數據創建一個blob

$http.post('/fetchBlobURL',{myParams}, {responseType: 'arraybuffer'}) 
    .success(function (data) { 
     var file = new Blob([data], {type: 'application/pdf'}); 
     var fileURL = URL.createObjectURL(file); 
     window.open(fileURL); 
}); 

的更多信息:Sending_and_Receiving_Binary_Data

+0

不適合我。在瀏覽器窗口中輸入URL時,它可以工作,但不能通過$ http.post方法。還有什麼要注意的嗎?安全設定?瀏覽器類型和版本? – zszep

+0

現在正在工作。似乎arrayBuffer在angularjs中是以v.1.1的形式實現的。我有v.1.08。升級angularjs時,一切正常。 – zszep

+0

這幾乎適用於我。我需要做同樣的事情,但我必須設置一個用戶友好的URL或文件名。瀏覽器設置它的UUID對我來說不起作用。你知道是否有辦法在新選項卡中打開PDF並在URL中設置文件名?謝謝! – elfrasco

6

我使用AngularJS v1.3.4

HTML:

<button ng-click="downloadPdf()" class="btn btn-primary">download PDF</button> 

JS控制器:

'use strict'; 
angular.module('xxxxxxxxApp') 
    .controller('MathController', function ($scope, MathServicePDF) { 
     $scope.downloadPdf = function() { 
      var fileName = "test.pdf"; 
      var a = document.createElement("a"); 
      document.body.appendChild(a); 
      MathServicePDF.downloadPdf().then(function (result) { 
       var file = new Blob([result.data], {type: 'application/pdf'}); 
       var fileURL = window.URL.createObjectURL(file); 
       a.href = fileURL; 
       a.download = fileName; 
       a.click(); 
      }); 
     }; 
}); 

JS服務:

angular.module('xxxxxxxxApp') 
    .factory('MathServicePDF', function ($http) { 
     return { 
      downloadPdf: function() { 
      return $http.get('api/downloadPDF', { responseType: 'arraybuffer' }).then(function (response) { 
       return response; 
      }); 
     } 
    }; 
}); 

Java的REST帶狀片S ervices - Spring MVC的:

@RequestMapping(value = "/downloadPDF", method = RequestMethod.GET, produces = "application/pdf") 
    public ResponseEntity<byte[]> getPDF() { 
     FileInputStream fileStream; 
     try { 
      fileStream = new FileInputStream(new File("C:\\xxxxx\\xxxxxx\\test.pdf")); 
      byte[] contents = IOUtils.toByteArray(fileStream); 
      HttpHeaders headers = new HttpHeaders(); 
      headers.setContentType(MediaType.parseMediaType("application/pdf")); 
      String filename = "test.pdf"; 
      headers.setContentDispositionFormData(filename, filename); 
      ResponseEntity<byte[]> response = new ResponseEntity<byte[]>(contents, headers, HttpStatus.OK); 
      return response; 
     } catch (FileNotFoundException e) { 
      System.err.println(e); 
     } catch (IOException e) { 
      System.err.println(e); 
     } 
     return null; 
    } 
7

如果設置{ responseType: 'blob' },無需自己創建Blob。您可以簡單地創建基於響應內容的網址:

$http({ 
    url: "...", 
    method: "POST", 
    responseType: "blob" 
}).then(function(response) { 
    var fileURL = URL.createObjectURL(response.data); 
    window.open(fileURL); 
});