【问题标题】:Force file download once a site is visited访问网站后强制下载文件
【发布时间】:2016-12-28 06:45:56
【问题描述】:

我有一个简单的站点 example.com 和一个外部文件链接。我希望实现这个场景: 一旦用户访问 example.com,就会自动下载来自外部链接的文件。 请注意,我不希望用户单击某个链接,而只是在访问该站点后立即下载文件。因此<a href="link/to/file" download>Download</a> 不是我需要的。 提前致谢。

【问题讨论】:

标签: html web-services file web download


【解决方案1】:

只需创建一个链接,通过js点击即可:

<script>
window.onload = function(){
  var a = document.createElement("a");
  a.href = "link/to/file";
  a.download = true;
  a.click();
};
</script>

【讨论】:

  • 你真棒,谢谢 - 我很难真正得到我正在寻找的答案 - 不断得到其他结果
  • 您可以通过设置 a.download = "filename"; 编辑建议的名称以将文件保存为下载后的文件
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-07
相关资源
最近更新 更多