访问站点后强制下载文件

Blu*_*ter 1 html web-services file download web

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

Jon*_*lms 5

只需创建一个链接并通过 js 点击它:

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