admin管理员组

文章数量:1425789

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

<a href="" 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("", (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.

My goal is to let a user download a PDF from my server. The most mon 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.

Share Improve this question edited Jul 10, 2015 at 21:22 tbogatchev asked Jul 10, 2015 at 21:09 tbogatchevtbogatchev 1,6513 gold badges15 silver badges22 bronze badges 3
  • Possible duplicate – ODelibalta Commented Jul 10, 2015 at 21:12
  • search "monitor download progress with javascript" Most solutions seem to require server-side intervention. – Kevin B Commented Jul 10, 2015 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 Commented Jul 10, 2015 at 21:17
Add a ment  | 

1 Answer 1

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

});

本文标签: javascriptTrigger a browser download with JQueryStack Overflow