html可以在图像标签中正确读取unicode
我将电子与HTML,CSS和JS一起使用。对于我的桌面应用程序,我想使用IMG标签从本地存储中导入图像。但是,我的文件名包含一个Unicode(例如#)。当HTML尝试获取图像时,它将从Unicode开始从文件名开始的下半部分。请参阅下面的示例。
<img src= "path/to/my/example#file.png">
,这将变成SRC =“路径/TO/MY/MY/MY示例”
通过删除Unicode之后的零件 。 将我返回控制台中的err_file_not_found。
但是,使用%23可能是一种选择 不幸的是,该文件名是不可预测的,并且它会根据文件更改(我导入了几张图片)。因此,获取文件名并创建HTML标签是使用JavaScript自动化的。 Javascirpt有没有办法自动在字符串中捕获Unicode(文件路径)并强制执行,以便HTML可以读取它?
I am using electron with html, css and js. For my desktop app, I want to import an image from my local storage using an img tag. However, my file name contains an unicode (e.g. #). When, HTML try to fetch the image it gets rid of the second half part of the file name starting from the unicode.See the example below please.
<img src= "path/to/my/example#file.png">
This becomes, src= "path/to/my/example"
by deleting the part after the Unicode.
Returning me ERR_FILE_NOT_FOUND in console.
using %23 may be an option however,
The filename is unfortunately unpredictable and it changes based on the file (I import several pictures). So, obtaining the filename and creating the HTML tag is automized using javascript. Is there a way for the javascirpt to automatically catch unicode in a string (path to file) and enforce it so that html can read it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要“编码”也称为“百分比”所有特殊字符。在您的示例中,将
#
替换为%23
。您还错误地使用了:而不是属性中的
=
:You need to 'url encode' also known as 'percent encode' all special characters. In your example, replace the
#
with%23
.You also incorrectly used
:
instead of=
in the attribute:#
不是您称之为“ Unicode”。这只是一个普通的ASCII角色。但是,在URL中,它恰好是保留的字符字符,用来表示资源中的锚点,因此不是资源路径的一部分,这就是为什么您看到它被剥离了。因此,您必须URL符号(IE,百分比编码)
#
字符为%23
如果要在路径本身中使用,例如:因为您说您需要通过JavaScript生成此HTML,您需要将脚本解析为文件路径并根据需要编码任何保留字符。实际上,JavaScript为此目的具有内置功能:
encodeuri()
和encodeuricomponent()
。#
is not a "unicode" as you call it. It is just a plain ASCII character. But, within a url, it just happens to be a reserved character, used to denote an anchor within a resource, and so is not part of the resource's path, which is why you see it getting stripped off.So, you must url-encode (ie, percent-encode) the
#
character as%23
if it is meant to be used inside the path itself, eg:Since you said that you need to generate this HTML via JavaScript, you will need to have your script parse the file path and encode any reserved characters as needed. In fact, JavaScript has built-in functions for this exact purpose:
encodeURI()
andencodeURIComponent()
.