您如何自动使用javascript下载文件?

Zek*_*ung 3 html javascript image download href

因此,我一直在寻找并尝试找到一种当有人进入我的网站时自动下载文件的方法。我尝试使用标签下载,它可以工作,您只需单击即可下载它。像这样

<a href="pic.jpg" download>Download</a>
Run Code Online (Sandbox Code Playgroud)

但是我不想要那个。我希望它无需点击即可自动下载。我需要一些帮助!

Zet*_*rcl 6

另一种方法:

var a = document.createElement('a');
a.setAttribute('href', dataUri);
a.setAttribute('download', filename);

var aj = $(a);
aj.appendTo('body');
aj[0].click();
aj.remove();
Run Code Online (Sandbox Code Playgroud)


Dan*_*ams 5

如果它是实际文件(某些文件不会像JPG文件那样在浏览器中简单显示),则可以使用javascript或元重定向。

<script> document.location.href = 'yourfile.exe'; </script>

要么

<meta http-equiv="refresh" content="0; url=yourfile.exe">

但是我想知道您是否在谈论被问到用户是否要打开或保存文件(无论是JPG还是其他格式)?