124

I need to let users of an application open a folder by clicking a link inside a web page. The path of the folder is on the network and can be accessed from everywhere. I'm probably sure there is no easy way to do this, but maybe I'm mistaken?

Sebastien Lachance
  • 1,927
  • 2
  • 17
  • 24
  • 4
    I've answered below; this is a common requirement of enterprise web applications that is made nearly impossible by misguided security (it should be possible to enable file links in trusted http pages). I have only tested extensively on Windows. – Andrew Duffy May 13 '09 at 01:29

10 Answers10

119

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 on file: 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.
Andrew Duffy
  • 6,800
  • 2
  • 23
  • 17
  • 5
    in IE only if the UNC path and the website containing the link are in the same domain, that is to say only in the intranet. – Stefan Steiger Feb 27 '13 at 16:17
  • 7
    I think this is no longer correct - newer versions of IE seem to block this behavior just as Chrome/Safari/etc. – ZeekLTK May 27 '14 at 21:00
  • 1
    I just tried this in IE 11 and you can in fact open a local file without downloading it (as in open an already existing file path). This still does not exist in chrome. – Hohohodown Feb 20 '15 at 20:47
  • 5
    You can get this to work in Chrome via [LocalLinks Chrome extension](https://chrome.google.com/extensions/detail/jllpkdkcdjndhggodimiphkghogcpida) . Thanks to [this StackOverflow answer](http://stackoverflow.com/a/2436256/1181012) – Zach Johnson Jul 01 '15 at 15:36
  • 1
    @Andrew Duffy, is there any kind of documentation that says Google Chrome is blocking this behavior ? I would like to know more about this – clhy Sep 21 '15 at 10:48
  • [this](https://stackoverflow.com/posts/2199283/revisions) seems to give a slightly different answer. – HattrickNZ Jan 17 '18 at 01:47
  • @Andrew Duffy, Is it possible to open a file present in the local machine with a relative path? – Protagonist Mar 03 '23 at 10:01
9

The URL file://[servername]/[sharename] should open an explorer window to the shared folder on the network.

Gottlieb Notschnabel
  • 9,408
  • 18
  • 74
  • 116
highlycaffeinated
  • 19,729
  • 9
  • 60
  • 91
7

A bit late to the party, but I had to solve this for myself recently, though slightly different, it might still help someone with similar circumstances to my own.

I'm using xampp on a laptop to run a purely local website app on windows. (A very specific environment I know). In this instance, I use a html link to a php file and run:

shell_exec('cd C:\path\to\file');
shell_exec('start .');

This opens a local Windows explorer window.

Lucas Taulealea
  • 355
  • 2
  • 10
  • 1
    Promising, but when I run this in Firefox the tab hangs. (Seems to be churning the session or something, because I can access other sites, but this site seems to be hung -- even in other tabs!) – Stephen R Jun 20 '19 at 17:01
  • I just tested it in Firefox, it works for me, but I can't specify the folder in which to open, it only opens in the root directory of the php file. – Lucas Taulealea Jun 21 '19 at 00:05
  • 1
    @LucasTaulealea I wonder why this answer has not been upvoted more. I would just add a little correction: the `cd` part seems unnecessary, in my case using only `shell_exec('start C:\path\to\file');` works and supports both a folder or file – Kaddath Mar 24 '21 at 11:19
  • @Kaddah It probably hasn't been upvoted more because this solution only works when the webserver is running on the same computer as your web browser. Most people are probably looking for ways to serve Windows Share Files from a corporate network web site. – JamesHoux Jun 18 '21 at 17:44
6

Using file:///// just doesn't work if security settings are set to even a moderate level.

If you just want users to be able to download/view files* located on a network or share you can set up a Virtual Directory in IIS. On the Properties tab make sure the "A share located on another computer" is selected and the "Connect as..." is an account that can see the network location.

Link to the virtual directory from your webpage (e.g. http://yoursite/yourvirtualdir/) and this will open up a view of the directory in the web browser.

*You can allow write permissions on the virtual directory to allow users to add files but not tried it and assume network permissions would override this setting.

Bickie
  • 61
  • 1
  • 2
  • This is relevant today, as Chrome and newer versions of IE will block access to local file:// resources from non-file web pages. Additionally, this can be setup to run in IIS Express, although it must be added and run manually. – Schmuli Jul 10 '14 at 09:32
5

What I resolved doing is installing a local web service on every person's computer that listens on port 9999 for example and opens a directory locally when told to. My example node.js express app:

import { createServer, Server } from "http";

// server
import express from "express";
import cors from "cors";
import bodyParser from "body-parser";

// other
import util from 'util';
const exec = util.promisify(require('child_process').exec);

export class EdsHelper {
    debug: boolean = true;
    port: number = 9999
    app: express.Application;
    server: Server;

    constructor() {
        // create app
        this.app = express();
        this.app.use(cors());
        this.app.use(bodyParser.json());
        this.app.use(bodyParser.urlencoded({
            extended: true
        }));

        // create server
        this.server = createServer(this.app);

        // setup server
        this.setup_routes();
        this.listen();
        console.info("server initialized");
    }

    private setup_routes(): void {
        this.app.post("/open_dir", async (req: any, res: any) => {
            try {
                if (this.debug) {
                    console.debug("open_dir");
                }

                // get path
                // C:\Users\ADunsmoor\Documents
                const path: string = req.body.path;

                // execute command
                const { stdout, stderr } = await exec(`start "" "${path}"`, {
                    // detached: true,
                    // stdio: "ignore",
                    //windowsHide: true,    // causes directory not to open sometimes?
                });

                if (stderr) {
                    throw stderr;
                } else {
                    // return OK
                    res.status(200).send({});
                }
            } catch (error) {
                console.error("open_dir >> error = " + error);
                res.status(500).send(error);
            }
        });
    }

    private listen(): void {
        this.server.listen(this.port, () => {
            console.info("Running server on port " + this.port.toString());
        });
    }

    public getApp(): express.Application {
        return this.app;
    }

}

It is important to run this service as the local user and not as administrator, the directory may never open otherwise.
Make a POST request from your web app to localhost: http://localhost:9999/open_dir, data: { "path": "C:\Users\ADunsmoor\Documents" }.

xinthose
  • 3,213
  • 3
  • 40
  • 59
5

make sure your folder permissions are set so that a directory listing is allowed then just point your anchor to that folder using chmod 701 (that might be risky though) for example

<a href="./downloads/folder_i_want_to_display/" >Go to downloads page</a>

make sure that you have no index.html any index file on that directory

lock
  • 6,404
  • 18
  • 58
  • 76
  • This answer works. The "directory listing allowed" part is very important. If it's not allowed, you can enabled it but it's different for every server application. – Travis May 13 '09 at 01:21
  • Is it possible to open a file present in the local machine with a relative path using an anchor tag? – Protagonist Mar 03 '23 at 10:14
3

You can also copy the link address and paste it in a new window to get around the security. This works in chrome and firefox but you may have to add slashes in firefox.

Wyrmwood
  • 3,340
  • 29
  • 33
3

I was looking for File System Access API and ended up in this question.

I know that API doesn't allow one to open an html link to a folder, but it does allow for opening local folders and files. For more information, take a look here:

https://web.dev/file-system-access/

marcelocra
  • 2,094
  • 2
  • 24
  • 37
2

Does not work in Chrome, but this other answers suggests a solution via a plugin:

Can Google Chrome open local links?

Community
  • 1
  • 1
luison
  • 1,850
  • 1
  • 19
  • 33
2

Hope it will help someone someday. I was making a small POC and came across this. A button, onClick display contents of the folder. Below is the HTML,

<input type=button onClick="parent.location='file:///C:/Users/' " value='Users'>
Nagaraja JB
  • 729
  • 8
  • 18