Open a Direct File on the Hard Drive from Firefox (File:///)

Open a direct file on the hard drive from firefox (file:///)

Although i still think it is a programming question, it was answered here:
https://superuser.com/questions/103026/open-a-direct-file-on-the-hard-drive-from-firefox-file

Both Firefox and IE8 support the File URI scheme.

Here are some examples valid for
Windows systems, referring to the same
file c:\WINDOWS\clock.avi

file://localhost/c|/WINDOWS/clock.avi

file:///c|/WINDOWS/clock.avi

file://localhost/c:/WINDOWS/clock.avi

file:///c:/WINDOWS/clock.avi


While the last is the most obvious and
human-readable, the first one is the
most complete and correct one.

Apparently (from the same url):

Mozilla browsers refuse to follow file URLs on a page that it has fetched with the HTTP protocol.

but:

Mozilla browsers can be configured to override this security restriction as detailed in Mozillazine's "Links to Local Pages Don't Work".

Workaround for href= file://///... in Firefox

As it turns out, I was unaware that Firefox had this limitation/feature. I can sympathize with the feature, as it prevents a user from unwittingly accessing the local file system. Fortunately, there are useful alternatives that can provide a similar user experience while sticking to the HTTP protocol.

One alternative to accessing content via UNC paths is to publish your content using the WebDAV protocol. Some content managements systems, such as MS SharePoint, use WebDAV to provide access to documents and pages. As far as the end-user experience is concerned, it looks and feels just like accessing network files with a UNC path; however, all file interactions are performed over HTTP.

It might require a modest change in your file access philosophy, so I suggest you read about the WebDAV protocol, configuration, and permission management as it relates to your specific server technology.

Here are a few links that may be helpful if you are interested in learning more about configuring and using WebDAV on a few leading HTTP servers:

  • Apache Module mod_dav
  • IIS 7.0 WebDAV Extension
  • Configuring WebDAV Server in IIS 7, 6, 5

Link to file on local hard drive

If the file is outside of the scope of the web server's folder it will not be able to access the file to deliver it.

You can either create a file handler to deliver the files:

so change echo "<a href = file:///$newpath> $file </a>" . "<br>";

to echo "<a href = \"fileHandler.php?file=$file\"" . "<br>";

and create fileHandler.php as:

<?php
$file = $_GET['file'];
header('content-type:application/'.end(explode('.',$file)));
Header("Content-Disposition: attachment; filename=" . $file); //to set download filename
exit(file_get_contents($file));
?>

or bypass the web server and link to file directly (this will only work over LAN or VPN)

so change echo "<a href = file:///$newpath> $file </a>" . "<br>";

to echo "<a href ='$_SERVER[HTTP_HOST]/$newpath/$file'>$file</a><br />"

Mozilla Firefox File Type Issue

You need to add the Content-Disposition header to the response. Your server side code should look something like:

Response.Clear()
Response.ContentType = "application/CSV"
Response.AddHeader("content-disposition", "attachment; filename="" + filename + ".csv"")
...

Here is a similar question:

  • php - Filename of file when using firefox to download
  • Response Content type as CSV

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.



Related Topics



Leave a reply



Submit