简体   繁体   English

如何在 HTML 中创建下载链接?

[英]How can I create download link in HTML?

I have a basic idea of HTML.我对 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?如何创建链接以下载文件而不是访问它?

In modern browsers that support HTML5, the following is possible:在支持 HTML5 的现代浏览器中,可以进行以下操作:

<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.这将允许您更改实际下载的文件的名称。

This answer is outdated.这个答案已经过时了。 We now have the download attribute .我们现在有了download属性 (see also this link to MDN ) (另请参阅此链接到 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>

the target=_blank will make a new browser window appear before the download starts. target=_blank将在下载开始之前出现一个新的浏览器窗口。 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 (eg JPG or GIF images) will usually be opened within the browser.请注意,浏览器已知的文件类型(例如 JPG 或 GIF 图像)通常会在浏览器中打开。

You can try sending the right headers to force a download like outlined eg here .您可以尝试发送正确的标题以强制下载,例如此处概述。 (server side scripting or access to the server settings is required for that.) (为此需要服务器端脚本或访问服务器设置。)

In addition (or in replacement) to the HTML5's <a download attribute already mentioned,除了已经提到的 HTML5 的<a download属性之外(或替代),
the browser's download to disk behavior can also be triggered by the following http response header:浏览器的下载到磁盘行为也可以由以下 http 响应头触发:

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

This was the way to do before HTML5 (and still works with browsers supporting HTML5).这是 HTML5 之前的做法(并且仍然适用于支持 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:它的构造方式与任何其他链接的构造方式相同:

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

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

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:即使有嵌入式插件(Windows + QuickTime = ugh)也要强制下载,您可以在 htaccess / apache2.conf 中使用它:

AddType application/octet-stream EXTENSION

This thread is probably ancient by now, but this works in html5 for my local file.这个线程现在可能很古老,但这适用于我的本地文件的 html5。

For pdfs:对于 pdf:

<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).这应该在新窗口中打开 pdf 并允许您下载它(至少在 Firefox 中)。 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所以它就像

<p><a href="images/logo2.png" download>test pdf</a></p>

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.但是..这是基于检查 URL 文件名上的扩展名!您不想摆弄服务器的扩展名映射,因为您想以两种不同的方式传递相同的文件。 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.因此,对于下载,您可以通过将文件软链接到对该扩展映射不透明的名称来欺骗它,指向它,然后使用下载的重命名功能来修复名称。

 <a target="_blank" download="realname.mp3" href="realname.UNKNOWN">Download it</a> <a target="_blank" href="realname.mp3">Play it</a>

I was hoping just throwing a dummy query on the end or otherwise obfuscating the extension would work, but sadly, it doesn't.我希望只是在最后抛出一个虚拟查询或以其他方式混淆扩展名会起作用,但遗憾的是,它没有。

The download attribute doesn't work in IE, it ignores the "download" completely. 下载属性在IE中不起作用,它完全忽略了“下载”。 The download doesn't work on Firefox if the href points to a remote site. 如果href指向远程站点,则下载在Firefox上不起作用。 So Odin's example doesn't work on Firefox 41.0.2. 所以Odin的例子不适用于Firefox 41.0.2。

The download attribute is new for the <a> tag in HTML5下载属性是 HTML5 中<a>标记的新属性

<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.我更喜欢第一个,它对于任何扩展都是更可取的。

You can download in the various way you can follow my way.您可以按照我的方式以各种方式下载。 Though files may not download due to 'allow-popups' permission is not set but in your environment, this will work perfectly尽管由于未设置“允许弹出窗口”权限,文件可能无法下载,但在您的环境中,这将完美运行

 <div className="col-6"> <a download href="https://www.w3schools.com/images/myw3schoolsimage.jpg" >Test Download </a> </div>

another one this one will also fail due to 'X-Frame-Options' to 'sameorigin'.由于“X-Frame-Options”到“sameorigin”,另一个这个也会失败。

 <a href="https://www.w3schools.com/images/myw3schoolsimage.jpg" download> <img src="https://www.w3schools.com/images/myw3schoolsimage.jpg" alt="W3Schools" width="104" height="142"> </a>

If you host your file in AWS, this may work for you.如果您在 AWS 中托管您的文件,这可能对您有用。 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.由于浏览器不支持同源下载链接,解决方法之一是将图片 URL 转换为 base64 URL。 Then, you can download it normally.然后就可以正常下载了。

    var canvas = document.createElement("canvas")
    var ctx = canvas.getContext('2d')

    var img = new Image()
    img.src = your_file_url + '?' + new Date().getTime();
    img.setAttribute('crossOrigin', '')

    var array = your_file_url.src.split("/")
    var fileName = array[array.length - 1]

    img.onload = function() {
        canvas.width = img.naturalWidth
        canvas.height = img.naturalHeight
        ctx.drawImage(img,
            0, 0, img.naturalWidth, img.naturalHeight,
            0, 0, canvas.width, canvas.height)

        var dataUrl = canvas.toDataURL("image/png", 1)

        var a = document.createElement('a')
        a.href = dataUrl
        a.download = fileName
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
    }

Like this像这样

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

So a file name.jpg on a site example.com would look like this所以网站 example.com 上的文件 name.jpg 看起来像这样

<a href="www.example.com/name.jpg">Image</a>

i know i am late but this is what i got after 1 hour of search我知道我迟到了,但这是我搜索 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); }
    }

    ?>

and for downloadable link i did this对于可下载的链接,我这样做了

<a href="index.php?file=file.pdf">Download PDF</a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM