使用React引用具有绝对路径的本地图像

huangapple go评论62阅读模式
英文:

React reference local image with absolute path with react

问题

I'm building a simple desktop app using tauri, with a rust backend and a react frontend.
我正在使用 Tauri 构建一个简单的桌面应用,其中包含 Rust 后端和 React 前端。

I'm passing the absolute path to a local image (on my device) from the backend to the frontend, and trying to use that path to display the image, but every time, the image isn't loaded and the console looks for the image inside the app folder:
我从后端将本地图像的绝对路径传递到前端,并尝试使用该路径来显示图像,但每次图像都无法加载,控制台在应用程序文件夹中寻找图像:

I tried different things but I haven't been able to find a way that works for me to make sure the path given is interpreted as an absolute path, do you have any solution to suggest?
我尝试了不同的方法,但未能找到适合我的方法来确保给定的路径被解释为绝对路径,您有什么解决方案可以建议吗?

英文:

I'm building a simple desktop app using tauri, with a rust backend and a react frontend.
I'm passing the absolute path to a local image (on my device) from the backend to the frontend, and trying to use that path to display the image, but every time, the image isn't loaded and the console looks for the image inside the app folder:

使用React引用具有绝对路径的本地图像

I tried different things but I haven't been able to find a way that works for me to make sure the path given is interpreted as an absolute path, do you have any solution to suggest?

答案1

得分: 1

我发现,ReactJS默认不允许我们从应用程序文件夹外部导入任何资源。

当我尝试通过我的设备上的绝对路径从C:/导入图像时,React返回了以下错误:

未找到模块:错误:您尝试导入C:/Users/admin/Pictures/Screenshots/a.png,它位于项目src/目录之外。不支持src之外的相对导入。
您可以将其移动到src/内,或从项目的node_modules/添加符号链接。

到目前为止,我找到的唯一方法是将图像加载为base64字符串,有两种实现方式:

  • 作为ES6模块导入图像
  • 使用<img src={require(...) />

注意: 如果将图像的绝对路径粘贴到浏览器地址栏,然后双击它,您可以看到带有前缀file:///的图像文件的“完整路径”。

请参考下面的示例代码:

import React from 'react';
import imgSrc from 'file://C:/Users/admin/Pictures/Screenshots/a.png';

export const Demo = () => {
  return (
    <div className='test-import-img-from-device'>
      <img src={imgSrc} />&nbsp;&nbsp;它起作用了<br /><br />

      <img src={require('file://C:/Users/admin/Pictures/Screenshots/a.png')} />&nbsp;&nbsp;它也起作用了<br /><br />

      <img src={'C:/Users/admin/Pictures/Screenshots/a.png'} />&nbsp;&nbsp;没有起作用<br /><br />

      <img src={'file://C:/Users/admin/Pictures/Screenshots/a.png'} />&nbsp;&nbsp;同样没有起作用<br />
    </div>
  )
}

结果:
使用React引用具有绝对路径的本地图像

在您的情况下,我认为<img src={require('file://...')} />可以解决您的问题。

希望这能帮助您!

英文:

I've just play around and found that ReactJS default doesn't allow us to import any resources outside our app's folder.

As I tried to import an img via an absolute path from C:/ on my device, React returned this error:

> Module not found: Error: You attempted to import C:/Users/admin/Pictures/Screenshots/a.png which falls outside of the project src/ directory. Relative imports outside of src/ are not supported.
You can either move it inside src/, or add a symlink to it from project's node_modules/.

So the only way I've found so far is to load the image as a base64 string, there are 2 ways to achieve that:

  • Import img as a ES6 module
  • Use &lt;img src={require(...) /&gt;

Notice: If you paste your absolute path of the img to browser's address bar then double click it, you can see the "full path" of the img file with the prefix file:///.

Please refer to my example code below:

import React from &#39;react&#39;;
import imgSrc from &#39;file://C:/Users/admin/Pictures/Screenshots/a.png&#39;;

export const Demo = () =&gt; {
  return (
    &lt;div className=&#39;test-import-img-from-device&#39;&gt;
      &lt;img src={imgSrc} /&gt;
      &amp;nbsp; &amp;nbsp; It worked
      &lt;br /&gt;&lt;br /&gt;

      &lt;img src={require(&#39;file://C:/Users/admin/Pictures/Screenshots/a.png&#39;)} /&gt;
      &amp;nbsp; &amp;nbsp; It also worked
      &lt;br /&gt;&lt;br /&gt;

      &lt;img src={&#39;C:/Users/admin/Pictures/Screenshots/a.png&#39;} /&gt;
      &amp;nbsp; &amp;nbsp; Not worked
      &lt;br /&gt;&lt;br /&gt;

      &lt;img src={&#39;file://C:/Users/admin/Pictures/Screenshots/a.png&#39;} /&gt;
      &amp;nbsp; &amp;nbsp; Also not worked
    &lt;/div&gt;
  )
}

Result:
使用React引用具有绝对路径的本地图像


In your case, I think &lt;img src={require(&#39;file://...&#39;) /&gt; would solve your issue.

Hope this can help!

huangapple
  • 本文由 发表于 2023年2月6日 06:58:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75356085.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定