What are the ways to make an html link open a folder
Do you want to open a shared folder in Windows Explorer? You need to use a file:
link, but there are caveats:
- Internet Explorer will work if the link is a converted UNC path (
file://server/share/folder/
). - Firefox will work if the link is in its own mangled form using five slashes (
file://///server/share/folder
) and the user has disabled the security restriction onfile:
links in a page served over HTTP. Thankfully IE also accepts the mangled link form. - Opera, Safari and Chrome can not be convinced to open a
file:
link in a page served over HTTP.
Open local folder from link
Linking to local resources is disabled in all modern browsers due to security restrictions.
For Firefox:
For security purposes, Mozilla applications block links to local files (and directories) from remote files. This includes linking to files on your hard drive, on mapped network drives, and accessible via Uniform Naming Convention (UNC) paths. This prevents a number of unpleasant possibilities, including:
- Allowing sites to detect your operating system by checking default installation paths
- Allowing sites to exploit system vulnerabilities (e.g., C:\con\con in Windows 95/98)
- Allowing sites to detect browser preferences or read sensitive data
for IE:
Internet Explorer 6 Service Pack 1 (SP1) no longer allows browsing a local machine from the Internet zone. For instance, if an Internet site contains a link to a local file, Internet Explorer 6 SP1 displays a blank page when a user clicks on the link. Previous versions of Windows Internet Explorer followed the link to the local file.
for Opera (in the context of a security advisory, I'm sure there is a more canonical link for this):
As a security precaution, Opera does not allow Web pages to link to files on the user's local disk
How can I create a link to a local file on a locally-run web page?
You need to use the file:///
protocol (yes, that's three slashes) if you want to link to local files.
<a href="file:///C:\Programs\sort.mw">Link 1</a>
<a href="file:///C:\Videos\lecture.mp4">Link 2</a>
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.
You cannot cross from http(s) to the file protocol
Modern versions of many browsers (e.g. Firefox and Chrome) will refuse to cross from the http(s) protocol to the file protocol to prevent malicious behaviour.
This means a webpage hosted on a website somewhere will never be able to link to files on your hard drive. You'll need to open your webpage locally using the file protocol if you want to do this stuff at all.
Why does it get stuck without file:///
?
The first part of a URL is the protocol. A protocol is a few letters, then a colon and two slashes. HTTP://
and FTP://
are valid protocols; C:/
isn't and I'm pretty sure it doesn't even properly resemble one.
C:/
also isn't a valid web address. The browser could assume it's meant to be http://c/
with a blank port specified, but that's going to fail.
Your browser may not assume it's referring to a local file. It has little reason to make that assumption because webpages generally don't try to link to peoples' local files.
So if you want to access local files: tell it to use the file protocol.
Why three slashes?
Because it's part of the File URI scheme. You have the option of specifying a host after the first two slashes. If you skip specifying a host it will just assume you're referring to a file on your own PC. This means file:///C:/etc
is a shortcut for file://localhost/C:/etc
.
These files will still open in your browser and that is good
Your browser will respond to these files the same way they'd respond to the same file anywhere on the internet. These files will not open in your default file handler (e.g. MS Word or VLC Media Player), and you will not be able to do anything like ask File Explorer to open the file's location.
This is an extremely good thing for your security.
Sites in your browser cannot interact with your operating system very well. If a good site could tell your machine to open lecture.mp4 in VLC.exe, a malicious site could tell it to open virus.bat in CMD.exe. Or it could just tell your machine to run a few Uninstall.exe files or open File Explorer a million times.
This may not be convenient for you, but HTML and browser security weren't really designed for what you're doing. If you want to be able to open lecture.mp4 in VLC.exe consider writing a desktop application instead.
How to open a windows folder when clicking on some link on a HTML page using Python
Try to use URI with file:
scheme like file:///C:/TestData/openfolder.html
in your html:
<a href="file:///C:/TestData/openfolder.html">Link to test data</a>
Here is article on using file URIs in Windows.
UPD (extraction from comments): Each browser has its own way to handle such urls. At least Internet Explorer 8 under Windows 7 opens links in Windows Explorer as was required by jags.
Finally, for dynamic pages the web server is required. If one is needed take a look at discussion on creating simple web services using python.
How to open a local folder in explorer through a Chrome App Mode window?
you can do this in your html file
<form action="file://ip_addr/shared_folder/">
<input type="submit" value="Go to folder" />
</form>
P.S. I think you must have the network share mounted on your computer, for example if you want to access to "192.168.1.55/file" you must at least mount a folder from 192.168.1.55 with the same credential of /file
Opening a mapped Windows Explorer folder using HTML hyperlink from Microsoft Edge
Edge requires mapped LAN locations to be specified in the UNC-format, while IE used to be more agnostic about it. For more information about UNC-paths check out this link.
<p><a href = "file:///P:\somenetworkfolder"> Some Network Folder (Only works in IE)</a></p>
<p><a href = "\\servername\path\somenetworkfolder"> Some Network Folder (Works in Edge and IE)</a></p>
Out of interest, if you want similar functionality in Chrome, you have to use an extension to grant the browser permission to open Windows Explorer. Check out the Local Explorer Chrome Extension, or write your own.
Open local files(file://) using Chrome
You can't access to files outside your server or "SandBox", sandbox include the files that user push to the browser or to your server.
If the access from browser to a pc files from web pages was possible, it would be a security problem.
Related Topics
How to Programmatically Set the Value of a Select Box Element Using JavaScript
Is There a Spec That the Id of Elements Should Be Made Global Variable
Get Dom Content of Cross-Domain Iframe
Submit Two Forms With One Button
What Does "./" (Dot Slash) Refer to in Terms of an HTML File Path Location
Get File Size, Image Width and Height Before Upload
Jquery Document.Createelement Equivalent
How to Communicate Between Iframe and the Parent Site
Is It Wrong to Place the ≪Script≫ Tag After the ≪/Body≫ Tag
How to Dynamically Change a Web Page'S Title
How to Append Text to a '≪Div≫'
Reading File Contents on the Client-Side in JavaScript in Various Browsers
Make Iframe Automatically Adjust Height According to the Contents Without Using Scrollbar
When to Use Functional Setstate
Do Html5 Custom Data Attributes "Work" in Ie 6
Navbar Dropdown (Collapse) Is Not Working in Bootstrap 5
How to Pass Parameters on Onchange of HTML Select
How to Validate Inputs Dynamically Created Using Ng-Repeat, Ng-Show (Angular)