198

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:

  • C:\Programs\sort.mw
  • C:\Videos\lecture.mp4

The problem is that I'd like the links to function as a shortcut to the file. I've tried the following:

<a href="C:\Programs\sort.mw">Link 1</a>
<a href="C:\Videos\lecture.mp4">Link 2</a>

... but the first link does nothing and the second link opens the file in Chrome, not VLC.

My questions are:

  1. Is there a way to adjust my HTML to treat the links as shortcuts to the files?

  2. If there isn't a way to adjust the HTML, are there any other ways to neatly link to files scattered throughout the hard drive?

My computer runs Windows 7 and my web browser is Chrome.

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
Brian Fitzpatrick
  • 2,443
  • 2
  • 14
  • 14

5 Answers5

321

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.

doppelgreener
  • 4,809
  • 10
  • 46
  • 63
  • 1
    Thanks Jonathan. Do you know if there's a way to "show file in folder" as an alternative? – Brian Fitzpatrick Aug 15 '13 at 04:38
  • 7
    @Brian Your browser can't interact with your OS in that way, and you should be extremely glad it cannot. – doppelgreener Aug 15 '13 at 04:45
  • 34
    Seems like Chrome won't download local files using the file:/// protocol anyway (giving you a `Not allowed to load local resource` error) – Loupax Mar 24 '14 at 16:09
  • 1
    I want to give a link like this "file:///..\..\sort.mw" so that it goes back two folders and gets the file there. Because we are using the word file in dropbox. So is there any solution. – Murtaza Munshi Oct 28 '15 at 09:55
  • 5
    It might also worth mentioning you can not link from a website (e.g. local dev server) to a local file. http://forums.mozillazine.org/viewtopic.php?f=9&t=1730 – nuala Feb 20 '16 at 23:30
  • Never use absolute link, use relative one. Example : ` – Moff452 Apr 17 '19 at 13:28
  • Should this solution still work in Chrome nowadays? I am requesting a file from a local drive, but the webpage is hosted on a different server. I am unsure if it's the framework I am using that is blocking this, or that it it not working at all. – Emma van de Vreugde Nov 27 '20 at 08:35
  • 1
    @EmmavandeVreugde You cannot move from http(s) to the file protocol in recent browsers. See the last paragraph of the opening section. – doppelgreener Nov 27 '20 at 14:12
  • I have not tried, but here: https://www.w3schools.com/howto/howto_html_download_link.asp is this download attribute. It works at least with relative paths and doesn't open the file in browser but opens a dialog. I guess this is new information, because this answer is seven years old currently. – Antti Tanskanen May 04 '21 at 10:10
  • @AnttiTanskanen The download attribute has no relevance to this issue, since the issue is about opening those files in the browser or an application of the author's choice. It's also actually existed for a long time—it gained full support in Firefox and Chrome the same year this answer was written. I haven't mentioned it since it just isn't relevant, opening a download dialog isn't the desired behaviour—they're already right there on your hard drive. – doppelgreener May 04 '21 at 13:02
  • @doppelgreener ok, you are right. I was here to find a way to download files with browser and didn't read the first post thoroughly. I thought it was about general downloading. – Antti Tanskanen May 06 '21 at 07:36
  • @doppelgreener How to add a relative path of a sibling file in the anchor tag to open it? – Protagonist Mar 01 '23 at 07:49
15

If you are running IIS on your PC you can add the directory that you are trying to reach as a Virtual Directory. To do this you right-click on your Site in ISS and press "Add Virtual Directory". Name the virtual folder. Point the virtual folder to your folder location on your local PC. You also have to supply credentials that has privileges to access the specific folder eg. HOSTNAME\username and password. After that you can access the file in the virtual folder as any other file on your site.

http://sitename.com/virtual_folder_name/filename.fileextension

By the way, this also works with Chrome that otherwise does not accept the file-protocol file://

Hope this helps someone :)

user3507261
  • 151
  • 1
  • 3
  • 1
    Thanks for answer! I've put file:/// before a path to a file however when I click on it happends nothing. I have to open it with Ctrl (in new tab). Why is it so? – Piotr Czyż Jul 30 '15 at 09:30
6

Janky at best

<a href="file://///server/folders/x/x/filename.ext">right click </a></td>

and then right click, select "copy location" option, and then paste into url.

Chet Meinzer
  • 1,691
  • 2
  • 21
  • 35
4

back to 2017:

use URL.createObjectURL( file ) to create local link to file system that user select;

don't forgot to free memory by using URL.revokeObjectURL()

pery mimon
  • 7,713
  • 6
  • 52
  • 57
  • The file argument here requires an actual file object constructed inside the browser client's JS runtime, meaning we'd have to have uploaded it to the browser client already (via a file upload form control for example). The URL generated is a blob URL for downloading or referencing that copy stored in memory inside the browser client's JS runtime. That's not going to help in creating a link to a local file; it's a link to a copy stored in memory (that otherwise won't behave differently once we try to access it). – doppelgreener Jul 26 '18 at 13:31
  • 1
    Exactly what I needed. Use File Input to select a file, then open it in a tab using createObjectURL. – Tony Lugg May 22 '20 at 22:53
0

I've a way and work like this:

<'a href="FOLDER_PATH" target="_explorer.exe">Link Text<'/a>
croxy
  • 4,082
  • 9
  • 28
  • 46
Maciej
  • 27
  • 1
  • That does not look like a valid target value, [even in Internet Explorer](https://msdn.microsoft.com/en-us/library/ms534659(v=vs.85).aspx). – doppelgreener Oct 13 '16 at 11:06
  • IE ignores (invalid) target (above). Following works fine with IE10, 11: Link to tmp on drive C. – primehunter Nov 09 '16 at 10:29
  • It work well for me, using IE and a network drive path like 'file://servername/path\to\folder' in place of FOLDER_PATH. If you omit 'target="_explorer.exe"' the folder opens in IE instead of explorer.exe and it looks pretty much like explorer. – JonP Apr 07 '17 at 13:24