如何在 HTML 中创建下载链接? [英] How can I create download link in HTML?

查看:98
本文介绍了如何在 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?

推荐答案

这个答案已经过时了.我们现在有 download 属性.(另请参阅此MDN 链接)

如果下载链接"是指要下载的文件的链接,请使用

If by "the download link" you mean a link to a file to download, use

  <a href="http://example.com/files/myfile.pdf" target="_blank">Download</a>

target=_blank 将在下载开始前显示一个新的浏览器窗口.当浏览器发现资源是文件下载时,通常会关闭该窗口.

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.

请注意,浏览器已知的文件类型(例如 JPG 或 GIF 图像)通常会在浏览器中打开.

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

这篇关于如何在 HTML 中创建下载链接?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆