如何使用 file: 方案在 html 中指定本地文件? [英] How to specify a local file within html using the file: scheme?

查看:54
本文介绍了如何使用 file: 方案在 html 中指定本地文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

这有效.但是(由于过于冗长的原因)我正在尝试使用 file: 方案,但是我无法进行任何工作.以下是我使用文件重写上述行的方法:

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 是我当前的 IP 地址)

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

但是找不到文件,应该如何使用file:scheme来指定?

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

推荐答案

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

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

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

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.

这篇关于如何使用 file: 方案在 html 中指定本地文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆