Force file download once a site is visited

六月ゝ 毕业季﹏ 提交于 2021-02-07 10:29:00

问题


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.


回答1:


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>


来源:https://stackoverflow.com/questions/39067284/force-file-download-once-a-site-is-visited

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!