Html 如何使用锚标签<a>下载文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21607309/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:50:18  来源:igfitidea点击:

How to download file using anchor tag <a>

html

提问by Coder17

Why is this not working:

为什么这不起作用:

download.html

下载.html

<a href="jsplogin.jar">download</a>

The jsplogin.jarfile is in the same folder has the download.htmlfile.

jsplogin.jar文件与该文件位于同一文件夹中download.html

Just yesterday it had worked.

就在昨天,它奏效了。

when I click the download link the file jsplogin.jarshould download but its trying to open the file in the browser. when I right clicked on the link and selected "save link" nothing is happening.

当我单击下载链接时,文件jsplogin.jar应该下载,但它试图在浏览器中打开文件。当我右键单击链接并选择“保存链接”时,什么也没有发生。

回答by captainrad

In HTML5, in most browsers you can add a 'download' attribute to the aelement.

在 HTML5 中,在大多数浏览器中,您可以向a元素添加“下载”属性。

for example:

例如:

<a href="http://www.example.com/index.html" download>Download</a>

Based on this question. How can I create download link in html?

基于这个问题。如何在 html 中创建下载链接?

回答by kallayya Hiremath

Use the "download" attribute:

使用“下载”属性:

<a href="jsplogin.jar" download>download</a>

回答by stackPusher

The download attribute didn't work for me, but this did:

下载属性对我不起作用,但这样做了:

<a href="myfile.csv" target="_blank">Download</a>

<a href="myfile.csv" target="_blank">Download</a>

The opens a new tab but downloads the file and closes the tab once it realizes it's not a file type it should render. In my case it was a .csv, I did not test with .jar but i imagine you'd get the same result.

打开一个新选项卡,但下载文件并在意识到它不是它应该呈现的文件类型后关闭该选项卡。就我而言,它是一个 .csv,我没有用 .jar 进行测试,但我想你会得到相同的结果。