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

发布于 2025-02-11 09:33:56 字数 64 浏览 1 评论 0原文

我有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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

彼岸花似海 2025-02-18 09:33:56

在支持HTML5的现代浏览器中,可以使用以下内容:

<a href="link/to/your/download/file" download>Download link</a>

您还可以使用此信息:

<a href="link/to/your/download/file" download="filename">Download link</a>

这将使您更改实际下载的文件的名称。

In modern browsers that support HTML5, the following is possible:

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

妄司 2025-02-18 09:33:56

这个答案已过时。现在,我们有 下载 attribute 。 (另请参阅此链接到mdn ) /p>

(另请参 通过“下载链接”,您是指向文件链接下载的链接,使用

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

target = _Blank将在下载启动之前出现一个新的浏览器窗口。当浏览器发现资源是文件下载时,该窗口通常会关闭。

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

您可以尝试发送正确的标头来强制下载,例如概述eg 在这里。 (服务器端脚本或访问服务器设置是需要的。)

This answer is outdated. We now have the download attribute. (see also this link to 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. 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.)

夜雨飘雪 2025-02-18 09:33:56

另外(或更换)到HTML5的&lt; a download属性已经提到,
浏览器的下载到磁盘行为也可以由以下HTTP响应标头触发:

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

这是在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:

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

This was the way to do before HTML5 (and still works with browsers supporting HTML5).

淡水深流 2025-02-18 09:33:56

下载链接将是您要下载的资源的链接。它以其他任何链接的方式构建:

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

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

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>
一向肩并 2025-02-18 09:33:56

要链接到文件,

<a href="...">link text</a>

请执行与任何其他页面链接相同的操作:即使它们具有嵌入式插件(Windows + QuickTime = ugh),也可以在HTACCESS / APACHE2.CONF:

AddType application/octet-stream EXTENSION

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:

AddType application/octet-stream EXTENSION
栀子花开つ 2025-02-18 09:33:56

到现在为止,这个线程可能是古老的,但是对于我的本地文件来说,这在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中)。对于任何其他文件,只需将其制作为文件名即可。对于图像和音乐,您需要将它们存储在与网站同一目录中。所以会像

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

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

<p><a href="images/logo2.png" download>test pdf</a></p>
入画浅相思 2025-02-18 09:33:56

您可以通过两种方式使用

<a href="yourfilename" download>Download</a>

它将在旧浏览器中下载使用原始名称的文件此选项

2nd

<a href="yourfilename" download="newfilename">Download</a>

在这里您可以选择重命名文件并使用其他名称下载

You can use in two ways

<a href="yourfilename" download>Download</a>

it will download file with original name In Old Browsers this option was not available

2nd

<a href="yourfilename" download="newfilename">Download</a>

Here You have option to rename your file and download with a different name

于我来说 2025-02-18 09:33:56

还有一个微妙的东西可以在这里提供帮助。

我想拥有允许浏览器播放和显示的链接,以及一个纯粹的下载链接。新的下载属性很好,但由于浏览器强迫播放或显示文件仍然非常强大,但无法一直工作。

但是..这是基于检查URL文件名上的扩展名!您不想使用服务器的扩展映射,因为您想两种不同的方式交付相同的文件。因此,对于下载,您可以通过将文件软化到该名称,该名称不透明该扩展名映射,指向它,然后使用下载的重命名功能来修复该名称。

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

我希望最后在最后扔一个虚拟查询,或者使扩展的混淆能够起作用,但可悲的是,事实并非如此。

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.

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

情释 2025-02-18 09:33:56

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

木落 2025-02-18 09:33:56

如果您将文件托管在AWS中,则可能对您有用。该代码非常容易理解。由于浏览器不支持相同的原始下载链接,因此解决图像URL的一种方法是将图像URL转换为base64 URL。然后,您可以正常下载它。

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

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.

    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)
    }
如日中天 2025-02-18 09:33:56

像这样

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

,示例上的文件名。

<a href="www.example.com/name.jpg">Image</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

<a href="www.example.com/name.jpg">Image</a>
您的好友蓝忘机已上羡 2025-02-18 09:33:56

知道我迟到了

 <?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>

i know i am late but this is what i got after 1 hour of search

 <?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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文