简体   繁体   中英

Trigger a browser download with JQuery

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.

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

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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