html可以在图像标签中正确读取unicode

发布于 2025-01-28 16:26:22 字数 446 浏览 1 评论 0原文

我将电子与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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

陈甜 2025-02-04 16:26:22

您需要“编码”也称为“百分比”所有特殊字符。在您的示例中,将替换为%23

您还错误地使用了:而不是属性中的=

<img src="path/to/my/example#file.png">

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:

<img src="path/to/my/example#file.png">
有木有妳兜一样 2025-02-04 16:26:22

不是您称之为“ Unicode”。这只是一个普通的ASCII角色。但是,在URL中,它恰好是保留的字符字符,用来表示资源中的锚点,因此不是资源路径的一部分,这就是为什么您看到它被剥离了。

因此,您必须URL符号(IE,百分比编码)字符为%23如果要在路径本身中使用,例如:

<img src="path/to/my/example%23file.png">

因为您说您需要通过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:

<img src="path/to/my/example%23file.png">

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() and encodeURIComponent().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文