How to Specify a Local File Within HTML Using the File: Scheme

How to specify a local file within html using the file: scheme?

The file: URL scheme refers to a file on the client machine. There is no hostname in the file: scheme; you just provide the path of the file. So, the file on your local machine would be file:///~User/2ndFile.html. Notice the three slashes; the hostname part of the URL is empty, so the slash at the beginning of the path immediately follows the double slash at the beginning of the URL. You will also need to expand the user's path; ~ does no expand in a file: URL. So you would need file:///home/User/2ndFile.html (on most Unixes), file:///Users/User/2ndFile.html (on Mac OS X), or file:///C:/Users/User/2ndFile.html (on Windows).

Many browsers, for security reasons, do not allow linking from a file that is loaded from a server to a local file. So, you may not be able to do this from a page loaded via HTTP; you may only be able to link to file: URLs from other local pages.

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.

Writing HTML file path names for local files

You’ll need to use href="../page.html" in your page2.html file.

I recommend you read up on what a URL is, especially the part Absolute URLs vs relative URLs

So a URL has different parts, beginning with the scheme like file or https.

Different parts of a URL explained. http  = scheme, www.example.com = Domain Name, 80 = Port, /path/to/my/file.html = Path to the file, ?key1=value1&key2=value2 = Parameters, #SomewhereInTheDocument = Anchor

Image from Mozilla at the before-mentioned URLs

You can skip certain parts at the beginning of a URL, which will give you a relative URL. These parts will be replaced by the user agent (the browser) from the current location.

For example, you can use scheme-relative URLs:

<a href="//myhost.example.com/page.html">

If the page containing that code is served via https, the link will also be completed to https: https://myhost.example.com/page.html. If it’s served via ftp, it will complete to ftp://myhost.example.com/page.html.

The same goes for other parts, and when referencing other pages from the same site, you would use path-relative URLs.

Absolute and relative paths

Now, concerning the path part of a URL, there is also a distinction between absolute paths and relative paths, just like in your operating system.

<a href="/page.html"> is using an absolute path. This means go to the root directory of the same drive or host, to find page.html.

In your case, the page2.html is delivered from file:///Users/myuser/Downloads/website/folder/page2.html.

So when you use a path beginning with / (absolute), it will refer to the root of the drive, so basically C:\ and complete to file:///page.html.

<a href="../page.html"> now is a relative path, relative to the current location. It’s saying go up one directory to find page.html.

So with the same location as before, this will complete to
file:///Users/myuser/Downloads/website/page.html.

Downloading a file using a file url

<a href="file:///C:/Users/user/Desktop/Strectching.jpg" target="_blank" download="Stretching.jpg">Download image</a>

It will open the file in a new tab and there you can download it. If the extension of the file is not familiar with the browser, it will prompt you whether to download it or open it with an application

Why can't I do img src= C:/localfile.jpg ?

It would be a security vulnerability if the client could request local file system files and then use JavaScript to figure out what's in them.

The only way around this is to build an extension in a browser. Firefox extensions and IE extensions can access local resources. Chrome is much more restrictive.



Related Topics



Leave a reply



Submit