我对HTML有一个基本的概念。我想在我的样本网站创建下载链接,但我不知道如何创建它。我如何制作一个下载文件的链接而不是访问它?
当前回答
我知道我迟到了,但这是我搜索1小时后得到的
<?php
$file = 'file.pdf';
if (! file) {
die('file not found'); //Or do something
} else {
if(isset($_GET['file'])){
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file); }
}
?>
对于可下载链接,我是这样做的
<a href="index.php?file=file.pdf">Download PDF</a>
其他回答
下载链接是指向您想要下载的资源的链接。它的构造方式与其他链接相同:
<a href="path to resource.name of file">Link</a>
<a href="files/installer.exe">Link to installer</a>
我知道我迟到了,但这是我搜索1小时后得到的
<?php
$file = 'file.pdf';
if (! file) {
die('file not found'); //Or do something
} else {
if(isset($_GET['file'])){
// Set headers
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
// Read the file from disk
readfile($file); }
}
?>
对于可下载链接,我是这样做的
<a href="index.php?file=file.pdf">Download PDF</a>
你可以用两种方式使用
<a href="yourfilename" download>Download</a>
在旧浏览器中,这个选项是不可用的
2nd
<a href="yourfilename" download="newfilename">Download</a>
在这里,您可以选择重命名您的文件,并下载与不同的名称
这个答案已经过时了。现在我们有了download属性。(另见MDN链接)
如果你所说的“下载链接”是指下载文件的链接,那就使用
<a href="http://example.com/files/myfile.pdf" target="_blank">Download</a>
target=_blank会在下载开始之前出现一个新的浏览器窗口。当浏览器发现该资源是一个文件下载时,该窗口通常会关闭。
请注意,浏览器已知的文件类型(例如JPG或GIF图像)通常会在浏览器中打开。
你可以尝试发送正确的标题来强制下载,比如这里。(需要服务器端脚本或访问服务器设置。)
除了前面提到的HTML5的<a download属性之外, 浏览器的下载到磁盘行为也可以由以下HTTP响应头触发:
Content-Disposition: attachment; filename=ProposedFileName.txt;
这是HTML5出现之前的做法(现在支持HTML5的浏览器仍然适用)。
推荐文章
- 使伸缩项目正确浮动
- 形式内联内的形式水平在twitter bootstrap?
- 自定义元素在HTML5中有效吗?
- 如何触发自动填充在谷歌Chrome?
- 创建圈div比使用图像更容易的方法?
- 为什么Chrome浏览器不正确地确定页面是在不同的语言,并提供翻译?
- 在网页上用鼠标模拟震颤(例如帕金森病)?
- Bootstrap抛出Uncaught错误:Bootstrap的JavaScript需要jQuery
- 如何改变文本区域的边框颜色:焦点
- 我如何设置背景颜色为文本的宽度,而不是整个元素的宽度,使用CSS?
- 如何删除和清除所有的本地存储数据
- 强制打开“另存为…”弹出打开文本链接点击PDF在HTML
- 如何修改标签文本?
- 是否有一个链接到GitHub下载文件的最新版本的存储库?
- 在HTML中还有其他有用的空格码吗,比如半空格的 , em-spaces, en-spaces等等?