简体   繁体   English

$ http PDF下载-无效文件

[英]$http PDF download - invalid file

I am trying to download a PDF file with $http service from AngularJS. 我试图从AngularJS下载带有$ http服务的PDF文件。

When I try to open the file it is invalid (Invalid Color Space) and the page ist blank. 当我尝试打开文件时,它无效(无效的色彩空间),并且页面空白。

Then I check: 然后我检查:

  1. From Firefox developer console - Network: I see the response object from the ajax call. 从Firefox开发者控制台-网络:我从ajax调用中看到响应对象。
  2. I set a breakpoint in my code: I see the response object returned from $http. 我在代码中设置了一个断点:我看到响应对象是从$ http返回的。

They are somehow different: ie 它们有所不同:即

  1. From developer console: 在开发者控制台中:

    "%PDF-1.4 % ......... “%PDF-1.4% .........

  2. From the $http: 从$ http:

    %PDF-1.4 %äãÏÒ......... %PDF-1.4%äãÏÒ.........

The same is happened to the whole document. 整个文档也是如此。

What am I doing wrong? 我究竟做错了什么?

PS code: PS代码:

downloadService.downloadPDF(response.url).then(
  function(response) {
    if (response) {
      var aTag = document.getElementById('downloadFile');
      aTag.setAttribute('href', 'data:application/pdf;charset=utf-8,' + encodeURIComponent(response));
      aTag.setAttribute('download', $scope.data.filename);
      aTag.click();
    }
  });

Response Headers: 响应标题:

Cache-Control:max-age=3600, must-revalidate
Content-Disposition:inline; filename=4550f2025f45cc2d14a403fbc1840160d4d052e4_598551722_300501.pdf
Content-Type:application/pdf
Date:Tue, 23 Oct 2018 11:17:44 GMT
Expires:0
Pragma:public
Server:Apache-Coyote/1.1
Set-Cookie:JSESSIONID=Y0kTkAV7GJLVRK1fKuZfffff; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/appbase; HttpOnly
Transfer-Encoding:chunked

encodeURIComponent does not do the needed BASE64 encoding encodeURIComponent不执行所需的BASE64编码

https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs

Have a look here: 在这里看看:

https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

Or have a link to the file on the server 或具有指向服务器上文件的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM