How can I open a local folder view by clicking on any link?
I tried many options like
<a href="file:///D:/Tools/">Open folder</a>
or
<a onclick="file:///D:/Tools/">Open folder</a>
or
<a onclick="window.open(file:///D:/Tools/)">Open folder</a>
HTML can be used to open a folder from our local storage. In order to open a folder from our local storage, use 'HREF' attribute of HTML. In the HREF attribute, we specify the path of our folder.
Essentially, what you need to do is open File Explorer, navigate to the file, folder, or library you want to hyperlink, and right-click on it to see its context menu. Then, if you're on Windows 10, select “Give access to” and click or tap on Specific people.
While in the browser, press Ctrl+O in Windows (Cmd+O on Mac) as in “Open” and double-click the appropriate file.
The following works in all browsers, but as always there are caveats.
Background:
"URL Shortcuts" are OS dependent. The following solution is for MS Windows due to a lack of standards between environments.
If you require linux support for the solution below, please see this article.
* I have no connection to the article, YMMV.
URL shortcuts come in two forms:
Files with .URL extensions are text based. Can be dynamically generated.
[InternetShortcut]
URL=file:///D:/
Files with .LNK extension are binary. They can be generated dynamically, but require iShelLinkInterface implementer. This is complicated by default OS restrictions, which rightfully prevent an IIS process from reaching Shell.
.URL is the recommended solution, as dynamic generation is viable across Web Languages/Frameworks and allows for a KISS implementation.
Overview/Recap:
Solution:
Provide a downloadable link (.URL or .LNK) to the resource. Browser behavior will be explained at end of post.
Option 1: Produce a .lnk file and save it to the server. Due to the binary nature of the .LNK file, this is not the recommended solution, but a pre-generated file should be viable.
Option 2: Produce a .url file and either save it to the server or dynamically generate it. In my situation, I am dynamically creating the .URL file.
Solution Details (.URL):
Add .url to the available MIME types in your web server.
For IIS open the site, choose MIME Types, and add the following:
File name Extension= .url
MIME type: application/internet-shortcut
Per @cremax ... For Webkit Browsers like Chrome on Apache Servers add this code to .htaccess or http.config:
SetEnvIf Request_URI ".url$" requested_url=url Header add Content-Disposition "attachment" env=requested_url
The .url file is a text file formatted as follows (again, this can be dynamically generated).
File Contents:
[InternetShortcut]
URL=file:///D:
Provide a link to the script that generates the .url file, or to the file itself.
If you've simply uploaded a .url file to your server, add the following to your HTML:
<a href="URIShortcut.url">Round-About Linking</a>
Browser Dependent Behavior
Chrome: Download/Save file.url then open
In Chrome, this behavior can be augmented by choosing the "Always open files of this type" option.
FireFox: Download/Save file.url then open
Internet Explorer: Click “Open” and go straight to directory (no need to save shortcut)
Internet Explorer has the preferred behavior, but Chrome and Firefox are at least serviceable.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With