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?
12 Answers
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.
-
1i used same code for download PFD file and i tested in all browser all are support but in safari this code is not working safari instead of download pdf file open in new tab. – Renish Khunt Feb 23 '15 at 08:14
-
10http://caniuse.com/#search=download%20attribute Works in Chrome, Edge, Firefox, Opera and latest Android 4.4+ browsers, and not in Internet Explorer and Safari. – Bart Verkoeijen Apr 01 '16 at 01:17
-
How do I do this if I am creating the link dynamically on a button click? – Nongthonbam Tonthoi Mar 10 '17 at 17:51
-
I've tried this but its not working though in my case its an xml file whose location is passed as href – akshay kishore Apr 03 '19 at 12:16
-
1@NongthonbamTonthoi cant you use javascript to dynamically assign the href to the a tag – akshay kishore Apr 03 '19 at 12:17
-
19This isn't necessarily going to work, as it is limited to same-origin URLs. – Nathan Jun 01 '20 at 20:44
-
3This is not working for me, I have a `.exe` file that I want users to download. However, tried to do this myself, to verify, but this ain't working. It will just bring me to the source code, which I don't want viewed. – Daniyal Warraich Mar 15 '21 at 16:34
-
Doesn't work for me on a jpg. Just opens the image in the browser – JobHunter69 Oct 10 '22 at 23:15
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.)
-
3why 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
-
2Please 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
-
-
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
-
-
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
-
-
2
-
@Pekka웃 Is there a way to provide a specific name to the file being downloaded? – Quick-gun Morgan Dec 27 '17 at 22:59
-
-
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
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).

- 1,347
- 16
- 27
-
5
-
-
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
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>

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

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

- 93
- 1
- 5
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.

- 161
- 2
- 8
-
both buttons in running code snipped are not working for me – Yevhenii Bahmutskyi Jun 29 '21 at 10:51
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

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

- 187
- 1
- 10

- 921
- 8
- 12
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)
}

- 423
- 4
- 7
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>

- 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
-
9Won'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
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>

- 2,799
- 12
- 48
- 100
-
9This 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
-
3Bad 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