[英]React: How to load an image by path that contains special characters?
I have tried to load an image by path that contains special characters like &@^.我试图通过包含 &@^ 等特殊字符的路径加载图像。
When I loaded the image as below, it didn't work.当我如下加载图像时,它不起作用。
<img src="file:///test/@#$%/0.png"/>
So, I've tried to use encodeURI(path), encodeURIComponent(path), but it didn't work too.因此,我尝试使用 encodeURI(path)、encodeURIComponent(path),但也没有用。
How can I do this?我怎样才能做到这一点?
You have to encode each level in your path (skip the /
characters) like this:您必须像这样对路径中的每个级别进行编码(跳过
/
字符):
const path = 'file:///test/@#$%/0.png'
const encodedPath = 'file://' + path.replace('file://', '').split('/').map((p) => encodeURIComponent(p)).join('/')
console.log(encodedPath)
// Output: file:///test/%40%23%24%25/0.png
Use the above output for your img
tag, it will be loaded.使用上面的 output 作为您的
img
标签,它将被加载。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.