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

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

问题描述

我正在加载一个托管在Apache服务器中的OS X上的html文件,该文件位于同一目录中的另一个html文件中。 pre> < a href =2ndFile.html>< button type =submit>本地文件< / button>

这有效。然而(由于原因太漫长)我正在尝试使用file:scheme,但我无法获得任何工作。这里是我如何使用file重写上面的行:

 < a href =file://192.168。 1.57 /〜User / 2ndFile.html>< button type =submit>本地文件< / button> 

(192.168.1.57是我目前的IP地址)

将它更改为以下内容也无效:

 < a href =file:// Name- Of-MacBookPro /〜User / 2ndFile.html>< button type =submit>本地文件< /按钮> 

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

解决方案

文件: URL方案引用客户机上的文件。 文件中没有主机名: scheme;你只需提供文件的路径。因此,本地机器上的文件应该是 file:///〜User2和File.html 。注意三个斜线; URL的主机名部分为空,所以路径开始处的斜杠紧跟在URL开头的双斜线之后。您还需要扩展用户的路径; 不会在文件中展开: URL。所以你需要 file:///home/User/2ndFile.html (在大多数Unix上), file:/// Users / User / (在Mac OS X上)或 file:/// C:/Users/User/2ndFile.html (在Windows上) 。

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


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?

解决方案

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.

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

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