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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12711584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:10:37  来源:igfitidea点击:

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

htmlurlfile-uri

提问by Gruntcakes

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:

我正在加载一个托管在 OS X 内置 Apache 服务器上的 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: 方案,但是我无法进行任何工作。这是我如何使用文件重写上面的行:

<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来指定?

回答by Brian Campbell

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

file:URL方案指的是在客户机上的文件。file:方案中没有主机名;您只需提供文件的路径。因此,您本地计算机上的文件将是file:///~User/2ndFile.html. 注意三个斜线;URL 的主机名部分为空,因此路径开头的斜杠紧跟在 URL 开头的双斜杠之后。您还需要扩展用户的路径;~file:URL中不展开。因此,您需要file:///home/User/2ndFile.html(在大多数 Unix 上)、file:///Users/User/2ndFile.html(在 Mac OS X 上)或file:///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 加载的页面执行此操作;您可能只能链接到file:来自其他本地页面的 URL。

回答by arkascha

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

问题是你如何加载第一个文件。这真的是使用网络服务器完成的吗?听起来不像。如果是,那么为什么不使用相同的协议,很可能是 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" 之所以有效,是因为它使用了“相对 url”。这使得浏览器使用与他从哪里获取第一个(当前)文件相同的协议和路径。

回答by james turner

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 页面引用文件系统文件。您可以尝试在运行浏览器的机器上使用命令行中的文件路径来确认这是浏览器限制而不是合法的丢失文件。

回答by Mahmoud Elsonbati

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 文件