I have a simple site example.com and an external file link. I want this scenario to be implemented:
Once a user visits example.com, a file from an external link is automatically downloaded.
Mind that I don't want a user to click some link, but just an immediate file download once the site is visited. Thus <a href="link/to/file" download>Download</a>
is not what I need.
Thanks in advance.
Asked
Active
Viewed 4,403 times
2

Blues Tester
- 23
- 1
- 3
-
window.open("link/to/file"); – Jonas Wilms Aug 21 '16 at 18:05
-
http://stackoverflow.com/questions/22125501/trigger-an-automatic-download-from-javascript-doesnt-work-on-page-load – Nikita Aug 21 '16 at 18:07
1 Answers
7
Simply create a link and click on it via js:
<script>
window.onload = function(){
var a = document.createElement("a");
a.href = "link/to/file";
a.download = true;
a.click();
};
</script>

Jonas Wilms
- 132,000
- 20
- 149
- 151
-
1you rock thanks - i have had such a hard time trying to actually get the answer I was looking for - kept getting other results – Aurielle Perlmann Mar 06 '17 at 04:17
-
1You can edit the suggested name to save the file as once it downloads by setting a.download = "filename"; – Oceanescence Oct 17 '19 at 10:44