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. https://bgheavenly.weebly.com/blog/microsoft-policies-and-procedures. 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.
Active3 years, 2 months ago
One use of the download attribute is to set a different file download name than the actual link target itself. It stands to reason that if you can trigger the click event on hyperlinks using JavaScript code, you should be able to download multiple files at once. However, in practice, only Chrome.
But if I just link to the file with an Rory O'KaneRory O'Kane
<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?
![]()
20k88 gold badges6868 silver badges110110 bronze badges
1 Answer
Javascript download free for windows xp. Actually, your example is the proper way to link to the file:
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.
Download licence file for avast internet security. 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? Download solidworks for mac. 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.
Community♦
Rory O'KaneRory O'Kane
20k88 gold badges6868 silver badges110110 bronze badges
Not the answer you're looking for? Browse other questions tagged htmlhyperlinkdownload or ask your own question.
Active10 months ago
I'd like to have an html file that organizes certain files scattered throughout my hard drive. For example, I have two files that I would link to:
The problem is that I'd like the links to function as a shortcut to the file. Galaxy balls game free download. I've tried the following:
. but the first link does nothing and the second link opens the file in Chrome, not VLC.
My questions are:
My computer runs Windows 7 and my web browser is Chrome.
doppelgreener
4,54966 gold badges3535 silver badges5555 bronze badges
Brian FitzpatrickBrian Fitzpatrick
98122 gold badges77 silver badges1010 bronze badges
5 Answers
You need to use the
file:/// protocol (yes, that's three slashes) if you want to link to local files.
These will never open the file in your local applications automatically. That's for security reasons which I'll cover in the last section. If it opens, it will only ever open in the browser. If your browser can display the file, it will, otherwise it will probably ask you if you want to download the file.
Some browsers, like modern versions of Chrome, will even refuse to cross from the http protocol to the file protocol, so you'd better make sure you open this locally using the file protocol if you want to do this stuff at all.
Why does it get stuck without
|
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |