How can I make a url a download link in html? - 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 ?

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

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

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.
Download PDF
You'll also need to make sure that the path to the PDF file is correct on the href property of your anchor tag.

Use this (HTML) not PHP:
Download pdf

Use the full url including the pdf file like.
Download

Related

is it possible to make an hyperlink to only download a pdf file?

I have an hyperlink to a pdf form that can not be opened by the browser's pdf viewer. If clicked, the browser tries to show it but I get the error message like "it is necessary Acrobat Reader 8.x" etc. Is there a way to force an hyperlink to such pdf form file to only allow its downloading? In this way, the user could open it with his local Adobe Reader.
Let’s say you have a PDF that you want to let people download. The file will be like this:
Download Receipt
In most browsers, clicking on the link will open the file directly in the browser.
But, if you add the download attribute to the link, it will tell the browser to download the file instead.
<a href="/path/to/your/receipt.pdf" download>Download Receipt</a>
The download attribute works in all modern browsers, including MS Edge, but not Internet Explorer.
In the latest versions of Chrome, you cannot download cross-origin files (they have to be hosted on the same domain).
To make the hyperlink to download the pdf file when clicked, you should use download property inside the anchor tag. For example you can see the code below:
Download the pdf file
You can also give your own name to the downloadable pdf file in the download property that I provided as 'Document' in the code above.
Yes, it is possible. First download the file and then you'll see a link when it downloaded(it disappears quite quickly) just copy it and use:
hyperlink

PDF file not downloading with HTML5 download attribute

I have a download attribute on two different anchor tags. One is for an Excel file and one for PDF. The Excel file downloads. The PDF opens in new tab instead. Why is this happening? Code below:
<div class="col-2 center">
<a download href="files/excel-sample.xlsx">
<img src="img/excel-logo.png" />
</a>
</div>
<div class="col-2 center">
<a download href="files/pdf-sample.pdf">
<img src="img/pdf-logo.png" />
</a>
</div>
Download Basics
So essentially what is happening is that when you link to a file URL, the browser opens that URL and if it has accessibility to display the content, it almost always will. Some most common examples of this are image files (.png, .jpg, etc...). However, if the browser can't read the file type (.zip) then it will almost always download the content. A great way to force the browser to download the file is by adding the download attribute in the <a> tag.
PDFs are readable and accessible by most modern browsers so by default, the browser is set to open the file instead of download it. Because of this accessibility, most modern browsers have introduced settings that allow users to decide on a machine by machine basis whether or not a PDF (or any other readable file) should open in another window or download by default. In many cases, even with the download attribute, the browser can still decide for itself how to handle the file.
Possible Solutions
1 - If you are just trying to achieve the download functionality on your browser only (which it looks like you aren't but I thought I should include anyway), you can open chrome, go to Settings -> Advanced Settings -> Content Settings -> PDF Documents -> Toggle on Download
2 - You can compress and zip the file so the browser is forced to download the file.
3 - If you have root server access to your site and it is using Apache, you can add the following to your .htaccess
ForceType application/octet-stream
Header set Content-Disposition attachment
If you are using an NGINX web server, you can add the following redirect
location ~* (.*\.pdf) {
types { application/octet-stream .pdf; }
default_type application/octet-stream;
}
So the solution is:
<a href="pdf-sample" download="pdf-samle.pdf">
Note that "href" has a value without the .pdf reduction, but "download" has a value with the .pdf reduction. Remember you also have to remove the .pdf reduction from the targeted file. As it seems the browser treats the downloaded file as a non pdf file and therefore is not opening it by default.
you cannot use <a download href="files/pdf-sample.pdf">
but you can use <a href="files/pdf-sample.pdf"> for preview your file

How to make HTML Link which makes the user download an .html file

The following code will make the user download the .doc file
Download
I want to create a link that will let user to download .html file
Any Ideas how to do it....???
You can add a download atributte to the <a> tag.
We'll get something like this:
<a href="file.html" download>Download</a>
Note: The download attribute IS NOT supported in Internet Explorer.In this case, it's better to zip the html file (add it into a .rar or a .zip, WinRAR can do this) and link it.
It'll be: Click me
I guess the easiest way to provide a .html file for download to a user would be to put it in a .zip or .rar format.
You can use the HTML5 Download attribute in the anchor tag.
Download attribute signifies that the resource it points to should be downloaded by the browser instead of navigating to it.
<a href="myfile.html" download> Download HTML file </a>
or, you can specify a name of the downloadable file by yourself
Download with specified name
Not all browsers support this HTML5 attribute, here is the detail of where it will work.

Force browser to open file instead of prompting download

When clicking a PDF link in Firefox and Chrome, the file will sometimes be opened for in-browser viewing and sometimes prompt a "Save as" dialog.
If I wanted to force the link to always prompt a download I could use the download HTML5 attribute.
However, I want to do the opposite. I.e., force the links to always be viewed in the browser.
Sort of an inverse download attribute. Is there such a thing? :)
I'd prefer to not modify response headers when serving PDF documents - I want to be able to specify in markup what the browser behavior should be.
Thanks!
You can achieve that by setting the appropriate header (for instance, in case of PDF, the header will be Content-type: application/pdf;
With this header, the browser will know the mime-type of the file and display it if it is compatible with it.
Here you can see the headers for a PDF.
As a hint, what I like to do is to use some sort of controller (in case you are using a backend language) that handles the download. Hence, to download myNewProject.pdf I do
<a href='download.php?file=myNewProject.pdf&viewInBrowser=1'>Download!</a>
Then I can set the appropriate headers depending on the file type, or if I want to force download or view it in the browser...
I'm using Firefox in XP. I went to the OPTIONS under Tools and found Portable Document Format. Click on it and it will allow you to change the way PDF files are handled.
open the file in a Microsoft Word and save as html.

How do I link to a file to download that a browser can’t view?

In HTML, how do you link to a file that the browser can only download, not view? For instance, say I have a zip file, my-program.zip. I want visitors to my website to be able to download that file when they click a link. The link would look like this: download my program. On my webserver, the HTML file and the zip file are in the same directory, so the relative path of the zip file is simply its filename.
But if I just link to the file with an <a href="my-program.zip"> tag, the browser wouldn’t recognize the link, right? Because browsers can’t open zip files. So what is the proper way to link to it?
Actually, your example is the proper way to link to the file:
download my program
You can never tell for sure that a browser can’t view a file. You just link to it; it’s up to the browser to do what they think best with it – display it, download it, or do something else. Don’t worry; browsers will generally do the right thing.
This follows the principle of the web that you don’t know what the browser will do with the files and pages you send it. You mentioned a ZIP file, but think of PDF files. They are like a ZIP file: they are not HTML, they are not made for a browser, and the browser might download it. But there are plugins such as Adobe PDF Reader and Schubert’s PDF Browser Plugin that show the contents of the PDF file right in the browser. Similarly, hypothetically, there might be a ZIP file viewer for the browser – it might show the user the contents of the ZIP file in the browser and let the user decide where to extract those contents.
Most browsers don’t have the hypothetical ZIP file viewer described, so the file will just download, like you wanted. But that doesn’t really matter; just write your link and everything will be okay.
The browser could do things other than viewing the file or downloading the file right away. It could also ask the user if they want to download the file. Or it could start downloading the file, detect a virus in it, and delete it right away. The point is, it’s up to the browser what it does with the file.
Note that this policy goes the other way. Your HTML pages look to the browser just like files look – they are both “resources”. “Resource” is the “R” in “URL”. When you visit an HTML page by visiting a URL, the browser thinks “this is an HTML resource. What should I do with this? Oh, I can display it in the main window – I’ll do that.” This is the same process as downloading a ZIP file after clicking a link to its URL, where it thinks “this is a ZIP resource. What should I do with this? I can’t display it – I guess I’ll start downloading it and open the downloads window so the user can see what happened.” Most browsers even let you download the HTML of a page just like a file, if you ask it to.
If you have multiple formats of your file, and want to let the browser choose the best one it can view, then you could set up a system using the HTTP Accept header. For instance, if you had both a ZIP and a RAR version of my-program, then you could make it so you link to just my-program and the browser chooses the version it likes best. But the setup for that can be complicated, and that kind of system isn’t usually necessary for just a file download. The Accept header is usually used to get the correct version of something that the browser is meant to view – for instance, the browser might choose an MP4 video file over a WMV video file because it doesn’t have any codec that can play embedded WMV videos.
If you want to force the browser to download a file even though the browser can probably view it on its own, see this question.