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?
-
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...– SalketerCommented Oct 3, 2012 at 15:27
-
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.– OsamaBinLoginCommented Oct 14, 2023 at 1:22
5 Answers 5
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.
-
2
-
To use a relative path, you just include the relative path, without any
file:
scheme or//
. So if you have a fileindex.html
, and want to refer toother_file.html
in the same directory, you would just link directly toother_file.html
with no scheme. Commented Mar 23, 2017 at 16:45 -
1@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?– RrptmCommented Feb 14, 2021 at 19:05
-
You need to ask a user to upload a file to your server explicitly, otherwise it looks like you are trying to hijack someone's data. Commented Nov 13, 2024 at 10:56
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.
-
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. Commented Oct 14, 2023 at 1:36
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.
-
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://.– PiepantsCommented Oct 3, 2012 at 15:35
-
1Well 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.– arkaschaCommented Oct 3, 2012 at 15:52
-
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 thefile
scheme. Commented Aug 12, 2022 at 15:23 -
@OrestisKapar It was the case, but none of that is relevant. Commented Aug 31, 2023 at 21:09
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
-
Did you check if you proposed solution works? If not, I recommend you do not post the answer, since it might mislead others. Commented May 15, 2018 at 6:06
-
Yes worked for me long time ago..editing my comment for more details Commented May 23, 2018 at 10:37
-
this should work
file://///ComputerNameOnNetwork/DriveLetter/index.html
– MeSo2Commented May 25, 2021 at 4:18
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