How can I create download link in HTML?

2019-01-01 10:16发布

I have a basic idea of HTML. I want to create the download link in my sample website, but I don't have idea of how to create it. How do I make a link to download a file rather than visit it?

标签: html download
10条回答
回忆,回不去的记忆
2楼-- · 2019-01-01 10:28

In addition (or in replacement) to the HTML5's <a download attribute already mentioned,
the browser's download to disk behavior can also be triggered by the following http response header:

Content-Disposition: attachment; filename=ProposedFileName.txt;

This was the way to do before HTML5 (and still works with browsers supporting HTML5).

查看更多
不流泪的眼
3楼-- · 2019-01-01 10:31

The download attribute is new for the <a> tag in HTML5

<a href="http://www.odin.com/form.pdf" download>Download Form</a>
or
<a href="http://www.odin.com/form.pdf" download="Form">Download Form</a>

I prefer the first one it is preferable in respect to any extension.

查看更多
步步皆殇っ
4楼-- · 2019-01-01 10:33

To link to the file, do the same as any other page link:

<a href="...">link text</a>

To force things to download even if they have an embedded plugin (Windows + QuickTime = ugh), you can use this in your htaccess / apache2.conf:

AddType application/octet-stream EXTENSION
查看更多
爱死公子算了
5楼-- · 2019-01-01 10:36

The download attribute doesn't work in IE, it ignores the "download" completely. The download doesn't work on Firefox if the href points to a remote site. So Odin's example doesn't work on Firefox 41.0.2.

查看更多
步步皆殇っ
6楼-- · 2019-01-01 10:38

In modern browsers that support HTML5, the following is possible:

<a href="link/to/your/download/file" download>Download link</a>

You also can use this:

<a href="link/to/your/download/file" download="filename">Download link</a>

This will allow you to change the name of the file actually being downloaded.

查看更多
无色无味的生活
7楼-- · 2019-01-01 10:38

A download link would be a link to the resource you want to download. It is constructed in the same way that any other link would be:

<a href="path to resource.name of file">Link</a>

<a href="files/installer.exe">Link to installer</a>
查看更多
登录 后发表回答