如何在HTML中创建下载链接?
我有HTML的基本思想。我想在我的示例网站上创建下载链接,但我不知道如何创建它。如何链接下载文件而不是访问该文件?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
在支持HTML5的现代浏览器中,可以使用以下内容:
您还可以使用此信息:
这将使您更改实际下载的文件的名称。
In modern browsers that support HTML5, the following is possible:
You also can use this:
This will allow you to change the name of the file actually being downloaded.
(另请参 通过“下载链接”,您是指向文件链接下载的链接,使用
target = _Blank
将在下载启动之前出现一个新的浏览器窗口。当浏览器发现资源是文件下载时,该窗口通常会关闭。请注意,浏览器已知的文件类型(例如JPG或GIF映像)通常会在浏览器中打开。
您可以尝试发送正确的标头来强制下载,例如概述eg 在这里。 (服务器端脚本或访问服务器设置是需要的。)
If by "the download link" you mean a link to a file to download, use
the
target=_blank
will make a new browser window appear before the download starts. That window will usually be closed when the browser discovers that the resource is a file download.Note that file types known to the browser (e.g. JPG or GIF images) will usually be opened within the browser.
You can try sending the right headers to force a download like outlined e.g. here. (server side scripting or access to the server settings is required for that.)
另外(或更换)到HTML5的
< a download
属性已经提到,浏览器的下载到磁盘行为也可以由以下HTTP响应标头触发:
这是在HTML5之前进行的方法(并且仍然可以与支持HTML5的浏览器一起使用。
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:
This was the way to do before HTML5 (and still works with browsers supporting HTML5).
下载链接将是您要下载的资源的链接。它以其他任何链接的方式构建:
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:
要链接到文件,
请执行与任何其他页面链接相同的操作:即使它们具有嵌入式插件(Windows + QuickTime = ugh),也可以在HTACCESS / APACHE2.CONF:
To link to the file, do the same as any other page link:
To force things to download even if they have an embedded plugin (Windows + QuickTime = ugh), you can use this in your htaccess / apache2.conf:
到现在为止,这个线程可能是古老的,但是对于我的本地文件来说,这在HTML5中起作用。
对于pdfs:
&lt; p&gt;&lt; a href =“ file:////.......... example.pdf”下载target =“ _ blank”&gt; test pdf&lt;/a&gt;/a&gt;&lt; p&gt;
这应该在新窗口中打开PDF,并允许您下载它(至少在Firefox中)。对于任何其他文件,只需将其制作为文件名即可。对于图像和音乐,您需要将它们存储在与网站同一目录中。所以会像
This thread is probably ancient by now, but this works in html5 for my local file.
For pdfs:
<p><a href="file:///........example.pdf" download target="_blank">test pdf</a></p>
This should open the pdf in a new windows and allow you to download it (in firefox at least). For any other file, just make it the filename. For images and music, you'd want to store them in the same directory as your site though. So it'd be like
您可以通过两种方式使用
它将在旧浏览器中下载使用原始名称的文件此选项
2nd
在这里您可以选择重命名文件并使用其他名称
下载You can use in two ways
it will download file with original name In Old Browsers this option was not available
2nd
Here You have option to rename your file and download with a different name
还有一个微妙的东西可以在这里提供帮助。
我想拥有允许浏览器播放和显示的链接,以及一个纯粹的下载链接。新的下载属性很好,但由于浏览器强迫播放或显示文件仍然非常强大,但无法一直工作。
但是..这是基于检查URL文件名上的扩展名!您不想使用服务器的扩展映射,因为您想两种不同的方式交付相同的文件。因此,对于下载,您可以通过将文件软化到该名称,该名称不透明该扩展名映射,指向它,然后使用下载的重命名功能来修复该名称。
我希望最后在最后扔一个虚拟查询,或者使扩展的混淆能够起作用,但可悲的是,事实并非如此。
There's one more subtlety that can help here.
I want to have links that both allow in-browser playing and display as well as one for purely downloading. The new download attribute is fine, but doesn't work all the time because the browser's compulsion to play the or display the file is still very strong.
BUT.. this is based on examining the extension on the URL's filename!You don't want to fiddle with the server's extension mapping because you want to deliver the same file two different ways. So for the download, you can fool it by softlinking the file to a name that is opaque to this extension mapping, pointing to it, and then using download's rename feature to fix the name.
I was hoping just throwing a dummy query on the end or otherwise obfuscating the extension would work, but sadly, it doesn't.
是
&lt; a&gt;
标记中的新属性下载属性 a&gt;
或
&lt; a href =“ http://www.odin.com/form.pdf”下载=“ form”&gt; download form&lt&lt&lt;/a&gt;
我更喜欢第一个在尊重方面优先到任何扩展。
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.
如果您将文件托管在AWS中,则可能对您有用。该代码非常容易理解。由于浏览器不支持相同的原始下载链接,因此解决图像URL的一种方法是将图像URL转换为base64 URL。然后,您可以正常下载它。
If you host your file in AWS, this may work for you. The code is very easy to understand. Because the browser doesn't support same-origin download links, 1 way to solve it is to convert the image URL to a base64 URL. Then, you can download it normally.
像这样
,示例上的文件名。
Like this
So a file name.jpg on a site example.com would look like this
知道我迟到了
我
i know i am late but this is what i got after 1 hour of search
and for downloadable link i did this