Download PDF file via XHR request

Is it possible to fully download a PDF file through an XHR request? I know that there are already many other discussions on this subject, but, unfortunately, I am still not happy with them. I use AngularJs and make a request using its $Http method. It does not return a file download popup. But if I click with the same URL in a new browser window, I get a popup. I already tried work-arround and its working mode, i.e. document.location.href = url; , but if I do, I won’t be able to show the pending image until the pop-up download window appears and appears. Therefore, not enough work for me. I would like to do this in an authentic way through a server request through which I can also process alternative result streams.

+7
javascript jquery html angularjs
source share
2 answers

Take a look at this jQuery jquery-file-download-plugin plugin and here is a demo page of this demo plugin. I think it dynamically inserts an iframe into the DOM and produces the look just like an AJAX request. This may be helpful for you.

+10
source share

This, of course, depends on the size of the PDF file, but it would be a workable approach if the PDF is not too large:

  • Show "pending image" and download the PDF using $http .

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

  • Redirect to this URL.
+4
source share

All Articles