Internet Explorer 的 URL 转义中文/日文 Unicode 字符
我正在尝试对我正在处理的几个 URL 中的非 ASCII 字符进行 URL 转义(百分比编码)。我正在使用一个 Flash 应用程序,该应用程序从这些 URL 加载图像和声音剪辑等资源。由于文件名可以包含非 ASCII 字符,如下所示: 日本语.jpg
我通过 utf-8 编码字符,然后对 unicode 字节进行百分比转义,得到以下内容:
%E6%97%A5%E6%9C%AC%E8%AA%9E.jpg
当我在 Internet Explorer 以外的任何浏览器中运行应用程序时,这些文件名都可以正常工作 - 我已经尝试过 Firefox、Safari 和 Chrome 但是当我在 IE 中启动应用程序(尝试了 6 和 8)并尝试加载声音剪辑时,我得到: 错误 #2044:未处理的 ioError
,并且 URL 已损坏为以下内容:
æ¥æØèª.jpg
关于如何解决此问题有什么想法吗?这只是使用本地文件系统 URL 测试驱动 Flash 应用程序。 我还注意到 Internet Explorer 无法找到以下文件: file:///C:/%E6%97%A5%E6%9C%AC%E8%AA%9E.jpg
,尽管 Chrome / Firefox 会对其进行解码并加载到文件中路径为
C:\日本语.jpg
edit
我认为我的问题与以下 ActionScript 代码片段中遇到的问题相同:
import flash.display.Loader;
import flash.net.URLRequest;
...
var ldr:Loader;
var req:URLRequest = new URLRequest("日本語.jpg");
ldr = new Loader();
ldr.load(req);
使用字符串 日本语.jpg
可在 IE 中使用,而使用字符串 %E6%97%A5%E6%9C%AC%E8%AA%9E.jpg
可在其他浏览器中使用。我需要的是一个可以在所有浏览器中使用的单一表单。我已经尝试过 %u
编码并将http请求标头设置为 Content-Type: text/html; charset=utf-8
无论是百分比转义还是非转义形式都没有运气。
I'm trying to URL-escape (percent-encode) non-ascii characters in several URLs I'm dealing with. I'm working with a flash application that loads resources like images and sound clips from these URLs. Since the filenames can contain non-ascii characters, like so:日本語.jpg
I escape them by utf-8 encoding the characters, and then percent-escaping the unicode bytes, to get the following:
%E6%97%A5%E6%9C%AC%E8%AA%9E.jpg
These filenames work fine when I run the app in any browser other than Internet Explorer - I've tried Firefox, Safari and Chrome. But when I launch the app in IE (tried both 6 and 8) and it tries to load the sound clip, I get:Error #2044: Unhandled ioError
, and the URL has been corrupted to something like:
æ¥æ¬èª.jpg
Any thoughts on how to fix this? This is just test-driving the flash app with local filesystem URLs.
I've also noticed that Internet explorer isn't able to locate a file such as:file:///C:/%E6%97%A5%E6%9C%AC%E8%AA%9E.jpg
, though Chrome / Firefox will decode it and load just fine for a file with the path
C:\日本語.jpg
edit
I think my problem is the same as the one encountered in the following ActionScript code fragment:
import flash.display.Loader;
import flash.net.URLRequest;
...
var ldr:Loader;
var req:URLRequest = new URLRequest("日本語.jpg");
ldr = new Loader();
ldr.load(req);
Using the string 日本語.jpg
will work in IE, while using the string %E6%97%A5%E6%9C%AC%E8%AA%9E.jpg
works in other browsers. What I need is a single form that will work in all browsers. I have tried the %u
encoding and setting the http request header to Content-Type: text/html; charset=utf-8
with no luck in either percent-escaped or unescaped form.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
抱歉,没有解决方案,但也许至少有一些有关这里可能发生的情况的更多信息。 (可能您已经弄清楚了这么多,但也许它会帮助其他读者找到解决方案。)“官方”url 编码规范似乎为如何解码转义的 url(如您生成的 URL)敞开了大门-- 转义实体是用来表示 UTF-8 字符(如 Firefox 等解释它们)还是 ASCII 字符(如 IE 解释它们)?我不知道有什么方法可以强制执行预期的解码策略。
只是一个问题:如果你根本不转义它们,而是将 unicode 留在 url 中,会发生什么坏事?虽然我对此没有太多经验,但我想我记得在某处读过,需要在 url 中转义 unicode 的日子已经过去了。可能是错的...
Sorry, no solution, but maybe at least some more information about what might be going on here. (Probably you've already figured this much out, but maybe it will help another reader find a solution.) The "official" url encoding specification seems to leave the door wide open as to how to decode escaped urls like the ones you are generating--are the escaped entities intended to represent UTF-8 characters (as Firefox, etc. are interpretting them) or ASCII characters (as IE is interpretting them)? I don't know of any way to force the intended decoding strategy.
Just a question: what bad thing is happening if you do not escape them at all, but leave the unicode in the url? Although I don't have a lot of experience with it, I thought I remember reading somewhere that the days of needing to escape unicode in urls are behind us. Could be wrong about that...
IE 对 HTTP URL 使用 UTF-8,但我不确定文件 URL(尽管大约 10 年前我作为 IE 团队的一部分测试了该行为)。如果您在 HTML 中使用 URL,我实际上建议尝试字符串文字(如果您的页面编码是 UTF-8)或数字字符引用 (&#dddd;)。 IE 通常会将字符转换为适当的编码,对于 HTTP 内容是 UTF-8,对于本地文件系统交互是 UTF-16。
实际上是 HTTP 需要 URL 转义,而不是 HTML 解析器。
IE uses UTF-8 for HTTP Urls, but I'm not sure about File URLs (even though I tested the behavior as part of the IE team about 10 years ago). If you are using the URLS in HTML, I'd actually recommend trying string literals (if your page encoding is UTF-8) or Numeric Character References (&#dddd;). IE will generally convert the characters into an appropriate encoding, which would be UTF-8 for the HTTP stuff, and UTF-16 for local file system interactions.
It's actually HTTP that needs the URL-escaping, not the HTML parser.
尝试仅对 URI 中会导致解析不正确的部分进行编码。例如,对 &、? 和空格进行编码。保持其他一切不变,它应该会像魅力一样发挥作用。
如果您仍然遇到问题,您可能需要在 http 标头中将内容类型设置为 utf。类似于内容类型:text/html;字符集=UTF-8。
Try encoding only the parts of the URI that would cause it to be parsed incorrectly. For instance, encode &, ?, and space. Leave everything else as is, and it should work like a charm.
If you are still running into problems, You may need to set the content-type to utf in your http headers. Something like Content-type: text/html; charset=UTF-8.
为什么不直接使用 Unicode 转义序列呢?将其粘贴到 HTML 网页的正文中,看看我的意思:
我得到 %u65E5%u672C%u8A9E.jpg。
Why not just use Unicode escape sequences? Paste this into a the body of an HTML web page to see what I mean:
I get %u65E5%u672C%u8A9E.jpg.
根据我的测试,我注意到 IE 不处理编码文件 URL,但它确实处理正常的 http URL,所以这可能是问题所在。我不确定你是如何加载它们的,但你应该检查一下这个问题。
From what i've tested, I noticed IE doesn't treat encoded file URLs but it does treat normal http URLs, so that could be the issue. I'm not sure how you are loading them, but you should check out that issue.
file:// 协议取决于您的操作系统区域设置,如果您的系统设置没有设置为中文而是英语,则不能让 IE 执行此操作。
file:// protocol depends on your OS region settings, if your system settings doesn't set to chinese but english, you can't let IE do this.