简体   繁体   English

如何在反应中加载src和公共文件夹之外的图像

[英]how to load image outside src and public folders in react

I want to reference my img tag src to the outside of the app, for instance from a local hard disk is it possible?我想将我的 img 标签 src 引用到应用程序的外部,例如从本地硬盘可以吗? in addition, in restful API how should reference img src to backend upload image folder.另外,在restful API中应该如何引用img src到后端上传图片文件夹。

If you reference an image outside of the app, you will be constrained to use full URL with domain (ex: https://google.com/your-path ).如果您在应用程序外部引用图像,您将被限制使用完整的 URL 和域(例如: https://google.com/your-path )。

In the case of an API, you can either use the full URL, or you can pass only the relative path (/your-path) and add the domain ( https://google.com ) as a variable in your frontend file (for example by fetching it from environment vars). In the case of an API, you can either use the full URL, or you can pass only the relative path (/your-path) and add the domain ( https://google.com ) as a variable in your frontend file (例如通过从环境变量中获取它)。

On your hard drive you can simply use something like在您的硬盘驱动器上,您可以简单地使用类似的东西

<img src={"../path/pictures/image.jpg"} />

With an API, you need to get the data and an url from where your image is stored.使用 API,您需要从存储图像的位置获取数据和 url。 Be it on the local hard drive of your server (which means you need to do something on your backend to serve the images) or a https:// url.无论是在服务器的本地硬盘驱动器上(这意味着您需要在后端做一些事情来提供图像)或https:// url。

Once you fetch your data from the backend, you can simply reference it in the <img/> balise.从后端获取数据后,您可以简单地在<img/>应答器中引用它。

<img src={state.url} />

If you're using Create-react-app you could learn here如果你使用 Create-react-app,你可以在这里学习

well, I solve problem.好吧,我解决问题。 in fact i convert imag file to strng like this:事实上,我将 imag 文件转换为 strng,如下所示:

String filePath = HostingEnvironment.MapPath(@"~/Images/"); //read file from directory

var imageString = System.IO.File.ReadAllBytes(filePath + obj.foodImage); // read file as string

 Transfer.Image  = imageString; // and pass to byte variable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM