284

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?

Tot Zam
  • 8,406
  • 10
  • 51
  • 76
Venkat
  • 20,802
  • 26
  • 75
  • 84

12 Answers12

547

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.

Eric Reed
  • 377
  • 1
  • 6
  • 21
Felix G.
  • 6,365
  • 2
  • 16
  • 24
186

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

Paolo
  • 20,112
  • 21
  • 72
  • 113
Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • 3
    why not use the download attribute, if you get a file like a jpg, it will download, instead of just opening. – Ben Oct 11 '14 at 00:46
  • 2
    Please omit the "target='_blank'", since that won't work in IE. Did you even test it? – Tara Dec 21 '14 at 21:39
  • 4
    @Dudeson please specify what "won't work" and which version(s) of IE you are talking about. (It is now safe to use the approach described TIIUNDER's much more recent answer below, though. It should get the accept mark.) – Pekka Dec 25 '14 at 23:51
  • @Pekka: IE11 and what doesn't work is, that no download dialog opens (this only happens when the site is online!). When clicking on the link nothing happens. Instead I'm using Oded's approach. That works in IE and FF (didn't test it in chrome). – Tara Dec 26 '14 at 19:24
  • its not letting download .sql/.aspx/.cshtml/.cs – Rush.2707 Oct 31 '15 at 10:11
  • I found that I didn't need the `target="_blank", without it it didn't redirect me, it just downloaded. Hope that was helpful! – Emmet Arries Mar 18 '16 at 04:28
  • This is not working in Chrome browswer. Is there any solution for it? – T8Z May 30 '16 at 02:47
  • Why has this answer been accepted? The browser will download the file or whatever there is on the link only if it cannot open the specific type. My browser will always open pdf files if it is not told to download it. The `download` attribute is the way to go here if we are talking about how to do it in html. It is supported by all major browsers. – Sergiu May 18 '17 at 12:19
  • 2
    @Sergiu the answer is seven years old. I can't delete it, and the asker hasn't responded to my request to switch the accept mark... nothing we can do, although I'll add a link to the more current answer – Pekka May 18 '17 at 12:50
  • Can I generate the file (new) name with PHP? Like with the current date? –  Jun 10 '17 at 20:59
  • 2
    @Dani see TIIUNDER's answer below, which is the correct one now. – Pekka Jun 10 '17 at 21:00
  • @Pekka웃 Is there a way to provide a specific name to the file being downloaded? – Quick-gun Morgan Dec 27 '17 at 22:59
  • @Quick-gunMorgan yes; see Felix' answer below – Pekka Dec 28 '17 at 00:11
  • Recently, April 2018, I found the target="_blank" helped render a PDF that I had in my Flask static files. Using just the download required a browser refresh. I know there are better programs to serve static content, however, I found this worked well for a simple prototype. – zerocog Apr 08 '18 at 22:36
27

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

Myobis
  • 1,347
  • 16
  • 27
  • 5
    But that requires a server side implementation, correct? – Lombas Nov 25 '15 at 16:01
  • @Lombas yes, only the server can set the http response headers. – Myobis Nov 26 '15 at 08:36
  • Is this the full answer? You also need to send a Content Type header and read the file to force the download. May want to and that to your answer. Full answer here: http://stackoverflow.com/a/1465631/2757916. – Govind Rai Sep 25 '16 at 07:10
  • this is perfect for server side implementation, just precise also the content type. it is well supported compared to the `download` attribute – william.eyidi Jun 22 '17 at 15:43
  • It may be possible to force by proxying the request to a service worker who can add this header but I'm not sure. – Mattwmaster58 Jul 14 '23 at 18:53
10

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>
Oded
  • 489,969
  • 99
  • 883
  • 1,009
10

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
Delan Azabani
  • 79,602
  • 28
  • 170
  • 210
  • Thanks! this is much simpler and is server-wide! :D I found this link that elaborates a little bit more. Thank you. http://www.htaccess-guide.com/adding-mime-types/ – Joe DF Apr 27 '14 at 01:36
  • That will make all files of that type download only. Fine if that's what you want, but could cause fits if you forget and want another file of that type to display in-browser instead of download. – TecBrat Sep 21 '15 at 18:08
7

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>
johan
  • 93
  • 1
  • 5
2

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.

jhhl
  • 161
  • 2
  • 8
2

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

0

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.

Ian Thompson
  • 187
  • 1
  • 10
Odin
  • 921
  • 8
  • 12
0

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)
    }
T H
  • 423
  • 4
  • 7
-2

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>
Con
  • 368
  • 1
  • 7
  • The problem with the latter is that it will open in the browser, not be offered for downloading and saving. – Pekka May 08 '10 at 10:53
  • 9
    Won't work; browser will treat it as a relative link to `./www.example.com/name.jpg` - you must use `http://` for absolute links with specified domain. – Delan Azabani May 08 '10 at 10:54
-8

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>
Sikander
  • 2,799
  • 12
  • 48
  • 100
  • 9
    This is terrible. When I see such a download link, I have to resist the urge to try index.php?file=index.php which in a copy/paste implementation of your code would eventually give a malicious person access to your database and who knows what. – M.Stramm Mar 31 '18 at 01:41
  • 3
    Bad idea. First, because you are making your server work more than necessary. Second, because the users can download any file :p – José Neto Aug 19 '18 at 09:07