访问网站后强制下载文件 [英] Force file download once a site is visited

查看:68
本文介绍了访问网站后强制下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的网站example.com和一个外部文件链接.我希望实现此方案:用户访问example.com后,会自动下载来自外部链接的文件.请注意,我不希望用户单击某些链接,而只是在访问该网站后立即下载文件.因此,< a href ="link/to/file" download> Download</a> 并不是我所需要的.预先感谢.

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.

推荐答案

只需创建一个链接,然后通过js单击它:

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>

这篇关于访问网站后强制下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆