如何使用file:scheme在html中指定本地文件?

时间:2022-09-04 08:06:49

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:

我正在加载一个托管在Apache服务器内置的OS X上的html文件,在该文件中我将链接到同一目录中的另一个html文件,如下所示:

<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:

这很有效。但是(由于原因太长而无法进入)我正在尝试使用file:scheme,但是我无法获得任何工作。以下是我使用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)

(192.168.1.57是我目前的IP地址)

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?

但无法找到该文件,应该如何使用file:scheme指定?

4 个解决方案

#1


31  

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).

文件:URL方案是指客户端计算机上的文件。文件中没有主机名:scheme;你只需提供文件的路径。因此,本地计算机上的文件将是file:///~User/2ndFile.html。注意三个斜杠; URL的主机名部分为空,因此路径开头的斜杠紧跟URL开头的双斜杠。您还需要扩展用户的路径; 〜不会扩展文件:URL。所以你需要file:///home/User/2ndFile.html(在大多数Unix上),file:///Users/User/2ndFile.html(在Mac OS X上),或者文件:/// C:/ Users / User / 2ndFile.html(在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.

出于安全原因,许多浏览器不允许从从服务器加载的文件链接到本地​​文件。因此,您可能无法通过HTTP加载的页面执行此操作;您可能只能链接到文件:来自其他本地页面的URL。

#2


2  

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

'文件'协议不是网络协议。因此file://192.168.1.57/~User/2ndFile.html根本没有多大意义。

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...

问题是如何加载第一个文件。这是真的使用Web服务器吗?真的听起来不像。如果是,那么为什么不使用相同的协议,很可能是http?您不能指望简单地切换协议并以相同的方式使用两种不同的协议......

I suspect the first file is really loaded using the apache 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.

我怀疑第一个文件是否真的使用apache服务器加载,但只是打开文件? href =“2ndFile.html”只是起作用,因为它使用“相对网址”。这使得浏览器使用与获取第一个(当前)文件的协议和路径相同的路径。

#3


1  

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.

“file://”url协议只能用于查找本地计算机文件系统中的文件。由于此html代码由浏览器解释,因此“本地计算机”是运行浏览器的计算机。

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.

如果您收到文件未找到错误,我怀疑是因为找不到该文件。但是,它也可能是浏览器的安全限制。某些浏览器不允许您从非文件系统html页面引用文件系统文件。您可以尝试在运行浏览器的计算机上使用命令行中的文件路径来确认这是浏览器限制而不是合法的丢失文件。

#4


0  

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

我以前有类似的问题,在我的情况下,文件是在另一台机器,所以我已将网络驱动器z映射到我的文件的文件夹位置,然后我在tomcat中创建了一个上下文,所以在我的web项目中,我可以通过上下文访问HTML文件

#1


31  

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).

文件:URL方案是指客户端计算机上的文件。文件中没有主机名:scheme;你只需提供文件的路径。因此,本地计算机上的文件将是file:///~User/2ndFile.html。注意三个斜杠; URL的主机名部分为空,因此路径开头的斜杠紧跟URL开头的双斜杠。您还需要扩展用户的路径; 〜不会扩展文件:URL。所以你需要file:///home/User/2ndFile.html(在大多数Unix上),file:///Users/User/2ndFile.html(在Mac OS X上),或者文件:/// C:/ Users / User / 2ndFile.html(在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.

出于安全原因,许多浏览器不允许从从服务器加载的文件链接到本地​​文件。因此,您可能无法通过HTTP加载的页面执行此操作;您可能只能链接到文件:来自其他本地页面的URL。

#2


2  

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

'文件'协议不是网络协议。因此file://192.168.1.57/~User/2ndFile.html根本没有多大意义。

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...

问题是如何加载第一个文件。这是真的使用Web服务器吗?真的听起来不像。如果是,那么为什么不使用相同的协议,很可能是http?您不能指望简单地切换协议并以相同的方式使用两种不同的协议......

I suspect the first file is really loaded using the apache 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.

我怀疑第一个文件是否真的使用apache服务器加载,但只是打开文件? href =“2ndFile.html”只是起作用,因为它使用“相对网址”。这使得浏览器使用与获取第一个(当前)文件的协议和路径相同的路径。

#3


1  

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.

“file://”url协议只能用于查找本地计算机文件系统中的文件。由于此html代码由浏览器解释,因此“本地计算机”是运行浏览器的计算机。

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.

如果您收到文件未找到错误,我怀疑是因为找不到该文件。但是,它也可能是浏览器的安全限制。某些浏览器不允许您从非文件系统html页面引用文件系统文件。您可以尝试在运行浏览器的计算机上使用命令行中的文件路径来确认这是浏览器限制而不是合法的丢失文件。

#4


0  

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

我以前有类似的问题,在我的情况下,文件是在另一台机器,所以我已将网络驱动器z映射到我的文件的文件夹位置,然后我在tomcat中创建了一个上下文,所以在我的web项目中,我可以通过上下文访问HTML文件