文件Uri方案和相关文件

use*_*419 58 uri file relative-path url-scheme file-uri

假设uri的方案是"文件".还假设路径以'.'开头.

示例路径是'./.bashrc'.fulluri看起来怎么样?'file://./.bashrc'对我来说很奇怪.

Ray*_*Luo 65

简而言之,文件URL采用以下形式:

file://localhost/absolute/path/to/file [ok]
Run Code Online (Sandbox Code Playgroud)

或者你可以省略主机(但不是斜杠):

file:///absolute/path/to/file [ok]
Run Code Online (Sandbox Code Playgroud)

但不是这个:

file://file_at_current_dir [no way]
Run Code Online (Sandbox Code Playgroud)

也不是这样

file://./file_at_current_dir [no way]
Run Code Online (Sandbox Code Playgroud)

我刚刚确认通过Python的urllib2.urlopen()

更多细节来自http://en.wikipedia.org/wiki/File_URI_scheme:

"file:///foo.txt" is okay, while "file://foo.txt" is not,
although some interpreters manage to handle the latter
Run Code Online (Sandbox Code Playgroud)

  • @RayLuo 仍然没有回答如何使用“.”的相对文件语法创建 URI 的问题。? (6认同)
  • @cogmission 你在我的回答中没有得到第四个例子吗?它清楚地提到没有办法使用“。” 在 URI 中。好吧,你可以,它只是没有任何意义,并且不会实现你可能期望的。您还可以参考 [本页中第二高的投票答案](/sf/answers/1344297951/)。但是,您阅读和弄清楚的时间更长。 (2认同)

kin*_*ana 23

使用完整文件:URI为'.'是不可能的.或路径中的".."段没有该路径的根部分.无论您使用'file://./.bashrc'还是'file:///./.bashrc',这些路径都没有意义.如果要使用相对链接,请在没有协议/权限部分的情况下使用它:

<a href="./.bashrc">link</a>
Run Code Online (Sandbox Code Playgroud)

如果要使用完整URI,则必须告知相对路径所在的根:

<a href="file:///home/kindrik/./.bashrc">link</a>
Run Code Online (Sandbox Code Playgroud)

根据RFC 3986

The path segments "." and "..", also known as dot-segments, are
defined for relative reference within the path name hierarchy.  They
are intended for use at the beginning of a relative-path reference
(Section 4.2) to indicate relative position within the hierarchical
tree of names.  This is similar to their role within some operating
systems' file directory structures to indicate the current directory
and parent directory, respectively.  However, unlike in a file
system, these dot-segments are only interpreted within the URI path
hierarchy and are removed as part of the resolution process (Section
5.2).

The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2).  However, some
deployed implementations incorrectly assume that reference resolution
is not necessary when the reference is already a URI and thus fail to
remove dot-segments when they occur in non-relative paths.  URI
normalizers should remove dot-segments by applying the
remove_dot_segments algorithm to the path, as described in Section 5.2.4.

The complete path segments "." and ".." are intended only for use
within relative references (Section 4.1) and are removed as part of
the reference resolution process (Section 5.2) 
Run Code Online (Sandbox Code Playgroud)

RFC 3986甚至描述了删除这些"."的算法.来自URI的"..".


and*_*ndz 14

在终端中,您可以使用"$ PWD"键入"file://$PWD/.bashrc"来引用当前目录.

  • 正如 @kai-dj 在另一个答案中提到的,如果 `$PWD` 包含像 `C:/Users/Joshua Pinter/` 这样的空格,那么路径将无效。需要以某种方式逃脱。 (2认同)

Max*_*nik 10

您不应在之后加上双斜线file:。正确的形式是

'file:.bashrc'
Run Code Online (Sandbox Code Playgroud)

参见RFC 3986path-rootless定义

  • 请参阅相同的 RFC,[语法组件的定义,§3](https://tools.ietf.org/html/rfc3986#section-3) 和 §3.2 权限(描述其相对组成,其中涉及`/ /`),然后记下 [RFC 的 §2 定义 `file:` 方案](https://tools.ietf.org/html/rfc8089#section-2),它只允许 `path-absolute`。相对的 `file:` URI 在技术上是不存在的,即使某些系统按照惯例允许它们。 (7认同)

Ken*_*Lin 6

我不知道你的用例。

我的节点代码也有类似的需求,所以当我需要一个相对于我的工作目录的文件 url 时,我创建了一个像这样的 url ......

const url = "file://" + process.cwd() + "/" + ".bashrc";
Run Code Online (Sandbox Code Playgroud)


Ale*_*sky 5

URI 始终是绝对的(除非它们是相对 URI,这是一种没有模式的不同野兽)。这是因为它们是一种服务器-客户端技术,引用服务器的工作目录是没有意义的。话又说回来,引用文件系统在服务器-客户端上下文中也没有意义。然而,RFC 8089只允许绝对路径:

路径部分表示文件系统中文件的绝对路径。

但是,如果我假设一个非标准扩展,我会选择以下语法:

file:file.txt
file:./file.txt
Run Code Online (Sandbox Code Playgroud)

解释是 RFC 8089 指定了非本地路径file://<FQDN of host>/path和本地路径file:/pathfile://localhost/path、 和file:///path。由于我们几乎肯定会尝试指定本地相对路径(即,可通过“本地文件系统 API”访问),并且 a.不是 FQDN,甚至不是主机名,因此简单的file:方案 + 方案特定部分 URI 语法使得最有意义。