最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Is it possible to force download a file (like pdf) as soon as a page loads - Stack Overflow

programmeradmin6浏览0评论

Actually this is what I'm trying to do. I have a form and when a user fills that form it goes to a thank you page where thank you message is displayed. Now what I'm trying to do is as soon as the thank you page opens a pdf file should start downloading without the user clicking anywhere on the page. I tried to find a solution by searching but almost all of them have given a solution where one link is provided and user have to click that link to download her pdf. But that is not what I want. And I want to achieve that using Javascript or jQuery only, no server side language.

A clarification:- PDF download on clicking a link is already working. What I want is PDF to download as soon as thank you page opens. I know logically this should not happen because by doing this anyone can set any number of files to be downloaded as soon as their page opens and fill your local drive. But my client wants only this thing to happen.

Actually this is what I'm trying to do. I have a form and when a user fills that form it goes to a thank you page where thank you message is displayed. Now what I'm trying to do is as soon as the thank you page opens a pdf file should start downloading without the user clicking anywhere on the page. I tried to find a solution by searching but almost all of them have given a solution where one link is provided and user have to click that link to download her pdf. But that is not what I want. And I want to achieve that using Javascript or jQuery only, no server side language.

A clarification:- PDF download on clicking a link is already working. What I want is PDF to download as soon as thank you page opens. I know logically this should not happen because by doing this anyone can set any number of files to be downloaded as soon as their page opens and fill your local drive. But my client wants only this thing to happen.

Share Improve this question edited Sep 17, 2015 at 4:46 user3224207 asked Sep 17, 2015 at 4:39 user3224207user3224207 4374 silver badges15 bronze badges 6
  • just call elm.click() on the link that downloads it... – dandavis Commented Sep 17, 2015 at 4:41
  • @dandavis:- You seem to have not read my question carefully. I don't want any link to be clicked. Pdf should download as soon as thank you page opens. Download by clicking a link is already working. – user3224207 Commented Sep 17, 2015 at 4:43
  • "Is it possible to force download a file (like pdf) as soon as a page loads" Not certain about meaning of "force download" ? – guest271314 Commented Sep 17, 2015 at 4:43
  • 2 i read it. if the human clicking works, then just click() for the user. the file will download when the mand is run, and the user won't have to click. simple, easy, effective... – dandavis Commented Sep 17, 2015 at 4:44
  • possible duplicate of Force download a pdf link using javascript/ajax/jquery – Imab Asghar Commented Sep 17, 2015 at 5:00
 |  Show 1 more ment

3 Answers 3

Reset to default 3

Calling click() for the user on an other-wise working link should do the trick:

<html>

<a href="/mypdf.pdf" download="mypdf.pdf" id="link">download pdf</a>

<script>
  var link = document.getElementById("link");
  link.click()
</script>
</html>

You can do it, in the HEAD tag:

<meta http-equiv="refresh" content="0; url=yourfile.pdf">

This will work also with browsers without JavaScript, and as soon as the the head tags loaded.

a working example:

See an example on github

You can use ajax to hit the url when the page loads.

EDIT: You might want to check this out https://stackoverflow./a/29266135/4549494

发布评论

评论列表(0)

  1. 暂无评论