How can I create download link in 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?

8,646 11 11 gold badges 53 53 silver badges 78 78 bronze badges asked May 8, 2010 at 10:48 21.4k 27 27 gold badges 76 76 silver badges 84 84 bronze badges

Is there any way to accomplish the same download behavior in a URL instead of a link? A URL that can be manually typed into a browser address bar?

Commented Jul 23 at 10:24

12 Answers 12

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

You also can use this:

This will allow you to change the name of the file actually being downloaded.

397 1 1 gold badge 6 6 silver badges 22 22 bronze badges answered May 1, 2013 at 10:56 6,661 2 2 gold badges 17 17 silver badges 24 24 bronze badges

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

Commented Feb 23, 2015 at 8:14

caniuse.com/#search=download%20attribute Works in Chrome, Edge, Firefox, Opera and latest Android 4.4+ browsers, and not in Internet Explorer and Safari.

Commented Apr 1, 2016 at 1:17 @NongthonbamTonthoi cant you use javascript to dynamically assign the href to the a tag Commented Apr 3, 2019 at 12:17 This isn't necessarily going to work, as it is limited to same-origin URLs. Commented Jun 1, 2020 at 20:44

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

Commented Mar 15, 2021 at 16:34

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

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

20.8k 21 21 gold badges 74 74 silver badges 119 119 bronze badges answered May 8, 2010 at 10:50 448k 148 148 gold badges 981 981 silver badges 1.1k 1.1k bronze badges

why not use the download attribute, if you get a file like a jpg, it will download, instead of just opening.

Commented Oct 11, 2014 at 0:46 Please omit the "target='_blank'", since that won't work in IE. Did you even test it? Commented Dec 21, 2014 at 21:39

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

Commented Dec 25, 2014 at 23:51

@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

Commented May 18, 2017 at 12:50 @Dani see TIIUNDER's answer below, which is the correct one now. Commented Jun 10, 2017 at 21:00

In addition (or in replacement) to the HTML5's 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).

answered Jul 11, 2014 at 9:32 1,347 16 16 silver badges 27 27 bronze badges But that requires a server side implementation, correct? Commented Nov 25, 2015 at 16:01 @Lombas yes, only the server can set the http response headers. Commented Nov 26, 2015 at 8: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: stackoverflow.com/a/1465631/2757916.

Commented Sep 25, 2016 at 7:10

this is perfect for server side implementation, just precise also the content type. it is well supported compared to the download attribute

Commented Jun 22, 2017 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.

Commented Jul 14, 2023 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:

Link Link to installer 
answered May 8, 2010 at 10:50 497k 101 101 gold badges 891 891 silver badges 1k 1k bronze badges

To link to the file, do the same as any other page link:

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 
answered May 8, 2010 at 10:53 Delan Azabani Delan Azabani 81.1k 29 29 gold badges 172 172 silver badges 212 212 bronze badges

Thanks! this is much simpler and is server-wide! :D I found this link that elaborates a little bit more. Thank you. htaccess-guide.com/adding-mime-types

Commented Apr 27, 2014 at 1: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.

Commented Sep 21, 2015 at 18:08

This thread is probably ancient by now, but this works in html5 for my local file.

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

answered Sep 4, 2015 at 2:26 93 1 1 silver badge 5 5 bronze badges

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.

Download it Play it

I was hoping just throwing a dummy query on the end or otherwise obfuscating the extension would work, but sadly, it doesn't.