最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PDF file download through XHR Request - Stack Overflow

programmeradmin0浏览0评论

Is it totally impossible to download a pdf file through an XHR request? I know there are many other discussions already on this topic but sadly, I am still not satisfied with them. I am using AngularJs and making a request by using its $Http method. It is not returning any file download popup. But if I hit with the same URL in a new Browser Window then I am getting a popup. I have already tried a work-arround and its working fine i.e. document.location.href = url; but If I do this then I am unable to show the waiting image till the time the download popup is ready and appeared. So the work arround is not enough for me. I would like to do it with a genuine way through a request to the server through which I can handle the alternative flows of the outcome as well.

Is it totally impossible to download a pdf file through an XHR request? I know there are many other discussions already on this topic but sadly, I am still not satisfied with them. I am using AngularJs and making a request by using its $Http method. It is not returning any file download popup. But if I hit with the same URL in a new Browser Window then I am getting a popup. I have already tried a work-arround and its working fine i.e. document.location.href = url; but If I do this then I am unable to show the waiting image till the time the download popup is ready and appeared. So the work arround is not enough for me. I would like to do it with a genuine way through a request to the server through which I can handle the alternative flows of the outcome as well.

Share Improve this question edited Nov 28, 2013 at 7:38 muneebShabbir 2,5384 gold badges30 silver badges49 bronze badges asked Nov 28, 2013 at 7:32 Ch Faizan MustansarCh Faizan Mustansar 3643 gold badges6 silver badges19 bronze badges 2
  • so in short you want to download pdf file using ajax request? – muneebShabbir Commented Nov 28, 2013 at 7:38
  • Yes, but in an angular.js or jQuery way... so that I can handle all alternative flows. like loading image ... – Ch Faizan Mustansar Commented Nov 28, 2013 at 7:40
Add a comment  | 

2 Answers 2

Reset to default 11

Have a look at this JQuery plugin jquery-file-download-plugin and here is the demo page of this plugin demo . I think its inserting iframe dynamically to DOM and produces look and feel just like AJAX request. It might be helpful for you.

It certainly depends on the size of the PDF file, but this would be a workable approach if the PDF isn't too large:

  1. Show the "waiting image" and download the PDF using $http.

    $http.get('http://my.example.com/foo.pdf').success(function(pdfData) {
      ... do something with pdfData ...
    });
    
  2. Convert pdfData to Base64 encoding and use a data URI scheme to create a URL for the downloaded PDF file.

  3. Redirect to that URL.
发布评论

评论列表(0)

  1. 暂无评论