1

My goal is to let a user download a PDF from my server. The most common approach to this would be to simply have a link with a download attribute like so

<a href="http://mysource.pdf" download>click here</a>

The problem with this is that I cannot monitor the download or have a callback when it is finished. The browser just hangs until the request is processed and suddenly pops up a download when it is done. So now I am getting pdf data from a server with AJAX and want to download the data via the browser.
So I have something along the lines of

$.get("http://mysource.pdf", (data) ->
  #need to somehow trigger a download with this data
)

I've been searching for a while to no avail. Any help is appreciated. Thanks!

--edit To clarify why this is not a duplicate, I am asking about triggering a download on data, not a local file.

tbogatchev
  • 1,531
  • 3
  • 14
  • 21
  • [Possible duplicate](http://stackoverflow.com/questions/1296085/download-file-using-jquery) – ODelibalta Jul 10 '15 at 21:12
  • search "monitor download progress with javascript" Most solutions seem to require server-side intervention. – Kevin B Jul 10 '15 at 21:13
  • I would like to point out that this is NOT a duplicate of the linked thread. That thread is asking about downloading a file. I am downloading data. – tbogatchev Jul 10 '15 at 21:17

1 Answers1

4
$('a').click(function(event) {
    event.preventDefault();  
    $.get("http://mysource.pdf", (data) ->
      window.location.href = $(this).attr('href');
    )

});
Keerthi
  • 923
  • 6
  • 22