How can I make a url a download link in html?

2019-07-17 16:15发布

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 ?

标签: html url
5条回答
闹够了就滚
2楼-- · 2019-07-17 16:41

HTML5 introduced the download attribute.

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

<a href="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

查看更多
走好不送
3楼-- · 2019-07-17 16:42

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.

<a href="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.

查看更多
你好瞎i
4楼-- · 2019-07-17 17:00

Use this (HTML) not PHP:

<a href="http://example.com/download/asdf.pdf">Download pdf</a>
查看更多
女痞
5楼-- · 2019-07-17 17:01

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

查看更多
Explosion°爆炸
6楼-- · 2019-07-17 17:05

Use the full url including the pdf file like.

<a href="http://example.com/download/file.pdf" target="_blank">Download</a>
查看更多
登录 后发表回答