html 空间显示为 %2520 而不是 %20 [英] A html space is showing as %2520 instead of %20

查看:30
本文介绍了html 空间显示为 %2520 而不是 %20的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将文件名传递给 firefox 浏览器会导致它用 %2520 而不是 %20 替换空格.

Passing a filename to the firefox browser causes it to replace spaces with %2520 instead of %20.

我在名为 myhtml.html 的文件中有以下 HTML:

I have the following HTML in a file called myhtml.html:

<img src="C:Documents and SettingsscreenshotsImage01.png"/>

当我将 myhtml.html 加载到 firefox 中时,图像显示为损坏的图像.所以我右键点击链接查看图片,它显示了这个修改后的 URL:

When I load myhtml.html into firefox, the image shows up as a broken image. So I right click the link to view the picture and it shows this modified URL:

file:///c:/Documents%2520and%2520Settings/screenshots/Image01.png
                    ^
                    ^-----Firefox changed my space to %2520.

什么鬼?它将我的空间转换为 %2520.它不应该将它转换为 %20 吗?

What the heck? It converted my space into a %2520. Shouldn't it be converting it to a %20?

如何更改此 HTML 文件以便浏览器可以找到我的图像?这是怎么回事?

推荐答案

稍微解释一下 %2520 是什么:

A bit of explaining as to what that %2520 is :

正如您自己指出的那样,公共空格字符被编码为 %20.% 字符被编码为 %25.

The common space character is encoded as %20 as you noted yourself. The % character is encoded as %25.

您获得 %2520 的方式是当您的 url 中已经有一个 %20 时,然后再次进行 urlencoded,从而转换 %20%2520.

The way you get %2520 is when your url already has a %20 in it, and gets urlencoded again, which transforms the %20 to %2520.

您(或您可能正在使用的任何框架)是否使用双重编码字符?

Are you (or any framework you might be using) double encoding characters?

对此进行一些扩展,特别是对于 LOCAL 链接.假设您要链接到资源 C:my pathmy file.html:

Expanding a bit on this, especially for LOCAL links. Assuming you want to link to the resource C:my pathmy file.html:

  • 如果你只提供本地文件路径,浏览器应该编码和保护所有给定的字符(在上面,你应该给它显示空格,因为 % 是一个有效的文件名字符,因此它将被编码)转换为正确的 URL 时(见下一点).
  • 如果你提供一个带有 file:// 协议的 URL,你基本上是在说明你已经采取了所有的预防措施并对需要编码的内容进行了编码,其余的应该被视为特殊字符.在上面的例子中,你应该提供 file:///c:/my%20path/my%20file.html.除了修复斜杠外,客户端不应在此处对字符进行编码.
  • if you provide a local file path only, the browser is expected to encode and protect all characters given (in the above, you should give it with spaces as shown, since % is a valid filename character and as such it will be encoded) when converting to a proper URL (see next point).
  • if you provide a URL with the file:// protocol, you are basically stating that you have taken all precautions and encoded what needs encoding, the rest should be treated as special characters. In the above example, you should thus provide file:///c:/my%20path/my%20file.html. Aside from fixing slashes, clients should not encode characters here.

注意:

  • 斜杠方向 - 正斜杠 / 在 URL 中使用,反斜杠 在 Windows 路径中使用,但大多数客户端将通过将它们转换为正确的正斜杠来使用两者.
  • 此外,协议名称后面有 3 个斜杠,因为您是在默默地引用当前机器而不是远程主机(完整的未缩写路径将是 file://localhost/c:/my%20path/my%file.html ),但同样假设您指的是本地机器并添加第三个斜杠,大多数客户端将在没有主机部分(即只有两个斜杠)的情况下工作.
  • Slash direction - forward slashes / are used in URLs, reverse slashes in Windows paths, but most clients will work with both by converting them to the proper forward slash.
  • In addition, there are 3 slashes after the protocol name, since you are silently referring to the current machine instead of a remote host ( the full unabbreviated path would be file://localhost/c:/my%20path/my%file.html ), but again most clients will work without the host part (ie two slashes only) by assuming you mean the local machine and adding the third slash.

这篇关于html 空间显示为 %2520 而不是 %20的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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