[英]How To make it so when you click an html button, it downloads a file to the persons computer?
<button type="submit" class="myButton" onclick="window.open('test.txt')">Money Dispenser</button>
Is the current code i have, and does not do anything at all when i click it.是我拥有的当前代码,当我单击它时根本不执行任何操作。 ( I want it to download test.txt to the computer or device of the user who cliked it) (我想让它把 test.txt 下载到点击它的用户的电脑或设备上)
您可以使用:
<a href="data:text/plain;charset=UTF-8,test.txt" download>Download</a>
I don't really understand what you are trying to achieve using a submit button, if it's not mandatory I'd use an <a>
.我真的不明白您要使用提交按钮实现什么,如果不是强制性的,我会使用<a>
。
download
attribute使用download
属性的纯 HTML5<a href="test.txt" download>Money Dispenser</a>
<button>
或者如果您必须使用<button>
,则使用 JavascriptThis has already been covered grealy in this question这已经在这个问题中被广泛涉及
But the gist of it is, create a function to download files as shown on that thread and add it to your <button>
但它的要点是,创建一个函数来下载该线程上显示的文件并将其添加到您的<button>
<button onclick="donwloaURI('data:text/plain,Test', 'Test.txt')">Money Dispenser</button>
You can use the HTML download
attribute to specify that the target will be downloaded when a user clicks on the hyperlink.您可以使用 HTML download
属性指定当用户单击超链接时将下载目标。
<a href="/download-file-path-here" download>
Money Dispenser
</a>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.