Skip to content Skip to sidebar Skip to footer

How Can I Make A Url A Download Link In Html?

A client wants a url to be a download link. Use case is like so: user gets linked to example.com/download once there, it downloads a pdf file. Can I do this without php ?

Solution 1:

HTML5 introduced the download attribute.

Supporting user-agents will offer to download the file foo.png when clicking this link:

<ahref="foo.png"download>Save the image</a>

You can also specify a different default file name that should be used:

<a href="foo.png" download="image.png>Save the image</a>

Read more at http://www.w3.org/TR/html5/links.html#downloading-resources.

Note that this only works for links. When users enter the URL directly into their browsers, this will have no effect, of course. If you want that, you need to send specific HTTP headers. See for example the question: How to force download of a file?. You don’t necessarily need a programming language like PHP for that. You can do it with, for example, .htaccess, too: Force File(image) Download with .htaccess

Solution 2:

How a file is displayed is browser specific. Some may force you to download while some directly render it on the browser.

If you want to force the browser to download the file then you can set in Header the

Content-Type : application/octet-stream

Solution 3:

You only need a link (anchor tag). The way the link behaves on click will depend on what browser you are and what settings you have in that particular browser. Some browsers will prompt you to open or save the file, other browsers will open the PDF file on a new tab or window.

<ahref="path/to/your/file.pdf">Download PDF</a>

You'll also need to make sure that the path to the PDF file is correct on the href property of your anchor tag.

Solution 4:

Use this (HTML) not PHP:

<ahref="http://example.com/download/asdf.pdf">Download pdf</a>

Solution 5:

Use the full url including the pdf file like.

<ahref="http://example.com/download/file.pdf"target="_blank">Download</a>

Post a Comment for "How Can I Make A Url A Download Link In Html?"