How do I link a file to a file that can be viewed in a browser?

In HTML, how do you link to a file that the browser can download rather than view? For example, let's say I have a zip file, my-program.zip. I want visitors to my website to be able to download this file when they click on the link. The link will look like this: download my program . On my web server, the HTML file and the zip file are in the same directory, so the relative path to the zip file is just its file name.

But if I just link to the file with the <a href="my-program.zip"> tag, the browser does not recognize the link, right? Because browsers cannot open zip files. So what is the correct way to communicate with him?

+4
source share
1 answer

Actually, your example is the correct way to link to a file:

 <a href="my-program.zip">download my program</a> 

You cannot say for sure that the browser cannot view the file. You simply refer to it; his browser can do what they consider best with it - display it, load it, or do something else. Do not worry; browsers usually do the right thing.

This follows the principle of the network that you do not know what the browser will do with the files and pages that you submit. You mentioned a ZIP file, but you thought about PDF files. They look like a ZIP file: they are not HTML, they are not designed for the browser, and the browser can download it. But there are plugins such as Adobe PDF Reader and Schuberts PDF Browser Plugin that display the contents of a PDF file directly in the browser. Similarly, presumably, there may be a ZIP file viewer for the browser β€” it can show the user the contents of the ZIP file in the browser and let the user decide where to extract it.

Most browsers do not have a hypothetical means of viewing ZIP files, so the file will just load as you like. But it does not really matter; just write your link and everything will be fine.

The browser can do something besides viewing the file or downloading the file immediately. He may also ask the user if he wants to download the file. Or he can start downloading the file, detect a virus in it and delete it right away. The fact is that before the browser it works with the file.

Please note that this policy goes a different way. Your HTML pages look the same in the browser as the files β€” they are both β€œresources”. "Resource" is the "R" in the "URL". When you visit an HTML page by visiting a URL, the browser considers that "it is an HTML resource. What should I do with this? Oh, I can display it in the main window - I do it." This is the same process as downloading a ZIP file after clicking on the link to its URL, where it believes that β€œthis is a ZIP resource. What can I do with this? I cannot display it, I think I started downloading it and open downloads so that the user can see what happened. " Most browsers even allow you to load an HTML page as a file if you ask for it.

If you have several formats of your file and you want the browser to choose the best one that it can view, you can configure the system using the HTTP protocol header . For example, if you had both a ZIP version and a RAR version of my program, then you could make sure that you only reference my-program , and the browser selects the version that suits it best. But the setup for this can be complicated, and such a system is usually not required to download a file. The Accept header is usually used to get the correct version of what the browser is intended for viewing - for example, the browser can select the MP4 video file through the WMV video file, because it does not have a codec that can play embedded WMV videos.


If you want to force the browser to download the file, although the browser may be able to view it on its own, see this question .

+11
source

All Articles