简体   繁体   English

如何在单击 html 按钮时将文件下载到个人计算机?

[英]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>

With pure HTML5 using the download attribute使用download属性的纯 HTML5

<a href="test.txt" download>Money Dispenser</a>

Or with Javascript if you have to use a <button>或者如果您必须使用<button> ,则使用 Javascript

This 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.

相关问题 如何制作一个按钮,当您单击它时会下载 excel 文件(phpspreadsheet) - How to make a button, that when you click it downloads an excel file (phpspreadsheet) 单击按钮时如何使 iframe 出现 - How do you make an iframe appear when you click a button 如何进行滚动,以便在滚动按钮时出现菜单 - How to make it so that when you scroll over a button a menu appears 如何使 HTML 网站具有交互性,以便当用户单击删除按钮时,即使您重新加载,它也会为所有用户更改? - How to make an HTML website interactive so that when the user clicks the delete button it changes for all users, even when you reload? 单击按钮时如何使图像缩放 - how to make an image zoom when you click a button 我如何做到这一点,以便当您单击代码标记的元素时,复制其 innerHTML - How do I make it so that when you click an elment that the code tag, you copy its innerHTML 如何在 html 中添加指向文本的链接(因此当您单击时会转到某个网站) - How to add link to text in html (so when you click you go to a website) 当我单击html或js中的下载按钮时,如何使“将文件保存到”选项 - How to make “save file to” option when i click on download button in html or js 如何制作它以便您无法单击“开始”提交表单(iPhone),但必须点击“提交”按钮? - How do I make it so that you cannot click “GO” to submit the form (iPhone), but must tap the “submit” button? 当你按下一个按钮时,你如何让一个按钮出现<input>在 HTML? 你如何制作一个在HTML中添加文本的按钮? - How do you make a button appear when you press an <input> in HTML? How do you make a button that adds text in HTML?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM