我对HTML有一个基本的概念。我想在我的样本网站创建下载链接,但我不知道如何创建它。我如何制作一个下载文件的链接而不是访问它?


当前回答

除了前面提到的HTML5的<a download属性之外, 浏览器的下载到磁盘行为也可以由以下HTTP响应头触发:

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

这是HTML5出现之前的做法(现在支持HTML5的浏览器仍然适用)。

其他回答

下载属性是HTML5中<a>标签的新属性

<a href="http://www.odin.com/form.pdf" download>下载表单</a> 或 <a href="http://www.odin.com/form.pdf" download="Form">下载表单</a> .

我更喜欢第一个,它在任何扩展方面都是可取的。

像这样

<a href="www.yoursite.com/theThingYouWantToDownload">Link name</a>

因此,example.com网站上的文件名。jpg将如下所示

<a href="www.example.com/name.jpg">Image</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="path to resource.name of file">Link</a>

<a href="files/installer.exe">Link to installer</a>

除了前面提到的HTML5的<a download属性之外, 浏览器的下载到磁盘行为也可以由以下HTTP响应头触发:

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

这是HTML5出现之前的做法(现在支持HTML5的浏览器仍然适用)。