Blues Tester
Blues Tester

Reputation: 23

Force file download once a site is visited

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.

Upvotes: 2

Views: 5303

Answers (1)

Jonas Wilms
Jonas Wilms

Reputation: 138267

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>

Upvotes: 7

Related Questions