javascript - Trigger a browser download with JQuery - Stack Overflow

My goal is to let a user download a PDF from my server. The most mon approach to this would be to simp

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');
    )

});

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745430512a4627367.html

相关推荐

  • javascript - Trigger a browser download with JQuery - Stack Overflow

    My goal is to let a user download a PDF from my server. The most mon approach to this would be to simp

    15小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信