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

5

71

I'm loading a html file hosted on the OS X built in Apache server, within that file I am linking to another html file in the same directory as follows:

<a href="2ndFile.html"><button type="submit">Local file</button>

This works. However (for reasons too lengthy to go into) I am experimenting using the file: scheme instead, however I cannot get anything to work. Here is how I am re-writing the above line using file:

<a href="file://192.168.1.57/~User/2ndFile.html"><button type="submit">Local file</button>

(192.168.1.57 is my current IP address)

Changing it to the following does also not work:

<a href="file://Name-Of-MacBookPro/~User/2ndFile.html"><button type="submit">Local file</button>

But the file cannot be found, how should it be specified using the file: scheme?

Sushi answered 3/10, 2012 at 15:18 Comment(2)
Unless I missed something, file:// Points to a file on the client machine... So if you are trying to get a file that is on the server, you should keep using HTTP...Reflate
None of your examples finish with </a>. So, all the html for the rest of the doc is included in the link. And, maybe other complications i can only imagine.Capp
W
94

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.

Wright answered 3/10, 2012 at 15:33 Comment(3)
how to use relative path instead of C:// on Windows?Iqbal
To use a relative path, you just include the relative path, without any file: scheme or //. So if you have a file index.html, and want to refer to other_file.html in the same directory, you would just link directly to other_file.html with no scheme.Wright
@Brian then there is no any way to download local file like file:///home/User/2ndFile.html ? I would like to know if there is any alternative available?Ottava
T
4

The 'file' protocol is not a network protocol. Therefore file://192.168.1.57/~User/2ndFile.html simply does not make much sense.

Question is how you load the first file. Is that really done using a web server? Does not really sound like. If it is, then why not use the same protocol, most likely http? You cannot expect to simply switch the protocol and use two different protocols the same way...

I suspect the first file is not really loaded using an apache http server at all, but simply by opening the file? href="2ndFile.html" simply works because it uses a "relative url". This makes the browser use the same protocol and path as where he got the first (current) file from.

Tinaret answered 3/10, 2012 at 15:31 Comment(4)
The first file is loaded by a client application, the reason I'm using file:// is I'm experimenting with some security stuff and seeing if its possible for the first file to use file://.Sushi
Well that's all fine (the little you tell). But if you use the file protocol this has nothing to do with apache. Apache is a http server.Tinaret
I realize that when this answer was written it may have not been the case: The file scheme is one of the schemes mentioned in the RFC1738 and defined in RFC8089. The URL Living Standard has examples using the file scheme.Whiteside
@OrestisKapar It was the case, but none of that is relevant.Shackelford
E
4

the "file://" url protocol can only be used to locate files in the file system of the local machine. since this html code is interpreted by a browser, the "local machine" is the machine that is running the browser.

if you are getting file not found errors, i suspect it is because the file is not found. however, it could also be a security limitation of the browser. some browsers will not let you reference a filesystem file from a non-filesystem html page. you could try using the file path from the command line on the machine running the browser to confirm that this is a browser limitation and not a legitimate missing file.

Eclair answered 7/12, 2016 at 18:47 Comment(1)
Yeah, there's no way a remote webpage would be allowed to use file: protocol. If so, any hacker could snoop around in your filesystem, just by getting you to surf to their page where they used those urls. Like file:///Users/turner/ might be your home directory, and file:///etc/passwd would be the list of all users on your machine, and file:///etc/ssh would have info on how to log in to your machine with ssh. (depends on your OS but easy to guess.). If this was EVER allowed, they probably prohibited it by 2000 in all browsers. No, no, no.Capp
B
1

I had similar issue before and in my case the file was in another machine so i have mapped network drive z to the folder location where my file is then i created a context in tomcat so in my web project i could access the HTML file via context

Brittney answered 15/5, 2018 at 5:48 Comment(3)
Did you check if you proposed solution works? If not, I recommend you do not post the answer, since it might mislead others.Superabound
Yes worked for me long time ago..editing my comment for more detailsBrittney
this should work file://///ComputerNameOnNetwork/DriveLetter/index.htmlEdraedrea
I
0

For apache look up SymLink or you can solve via the OS with Symbolic Links or on linux set up a library link/etc

My answer is one method specifically to windows 10.

So my method involves mapping a network drive to U:/ (e.g. I use G:/ for Google Drive)

open cmd and type hostname (example result: LAPTOP-G666P000, you could use your ip instead, but using a static hostname for identifying yourself makes more sense if your network stops)

Press Windows_key + E > right click 'This PC' > press N (It's Map Network drive, NOT add a network location)

If you are right clicking the shortcut on the desktop you need to press N then enter

Fill out U: or G: or Z: or whatever you want Example Address: \\LAPTOP-G666P000\c$\Users\username\

Then you can use <a href="file:///u:/2ndFile.html"><button type="submit">Local file</button> like in your question


related: You can also use this method for FTPs, and setup multiple drives for different relative paths on that same network.

related2: I have used http://localhost/c$ etc before on some WAMP/apache servers too before, you can use .htaccess for control/security but I recommend to not do so on a live/production machine -- or any other symlink documentroot example you can google

Intwine answered 29/10, 2020 at 4:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.