[英]displaying a local image in React by pressing a button and using it's local path
I'm following an online course and faced a tricky issue for me:我正在学习在线课程,但遇到了一个棘手的问题:
The final project (using React and an API) takes a link of an online 'jpg' photo so that when you press a button it displays that photo and recognize the faces on it.最终项目(使用 React 和 API)获取在线“jpg”照片的链接,以便当您按下按钮时,它会显示该照片并识别其上的面孔。 I want to apply this for my own local images on my PC as well but failed to figure out the way.
我也想将其应用于我自己的 PC 上的本地图像,但未能找到方法。 I need at least to be able to display a local image when providing it's local path then pressing the button.
在提供本地路径然后按下按钮时,我至少需要能够显示本地图像。 Please Help!
请帮忙!
The files are as follows (by brief of course):文件如下(当然是简短的):
App.js应用程序.js
import React from 'react'
import FaceRecognition from './components/FaceRecognition/FaceRecognition';
render() {
return (
<div className="App">
<ImageLinkForm onInputChange={this.onInputChange} onButtonSubmit={this.onButtonSubmit}/>
<FaceRecognition box={this.state.box} imageUrl={this.state.imageUrl}/>
</div>
);
}
export default App;
ImageLinkForm.js ImageLinkForm.js
import React from 'react'
const ImageLinkForm = ({onInputChange, onButtonSubmit}) => {
return (
<div>
<input className="f4 pa2 w-70 center" type="text" onChange={onInputChange}/>
<button className="w-30 grow f4 link ph3 pv2 dib white bg-light-purple" onClick={onButtonSubmit}>Detect</button>
</div>
)
}
export default ImageLinkForm
FaceRecognition.js人脸识别.js
import React from 'react'
const FaceRecognition = ({ imageUrl, box }) => {
return (
<div className="center ma">
<div className="absolute mt2">
<img id="inputimage" alt="" src={imageUrl} width="500px" height="auto"/>
</div>
</div>
)
}
export default FaceRecognition
So as you can see the imageUrl
is taken from the input text, and transferred as a parameter to FaceRecognition.js
, then it is given as src
to the img
.如您所见,
imageUrl
是从输入文本中获取的,并作为参数传递给FaceRecognition.js
,然后将其作为src
提供给img
。 My Problem is that if I'm using a local file like ( my_image.jpg
) this procedure is not working.我的问题是,如果我使用像(
my_image.jpg
)这样的本地文件,则此过程不起作用。 I put ( my_image.jpg
) in the input text and press the button but the photo is not displayed at all.我将(
my_image.jpg
)放入输入文本并按下按钮,但照片根本不显示。 I even tried ( ./my_image.jpg
), ( http://localhost:3000/image.jpg
), ( http://localhost/image.jpg
), ( file:///my_image.jpg
) and other stuff etc but never succeeded.我什至尝试过(
./my_image.jpg
)、( http://localhost:3000/image.jpg
)、( http://localhost/image.jpg
)、( file:///my_image.jpg
)和其他东西等,但从未成功。
Any idea how to fix this issue in React?知道如何在 React 中解决这个问题吗? I want to display a local image by using it's path in my application, so it is not a fixed image every time.
我想通过在我的应用程序中使用它的路径来显示本地图像,所以它不是每次都固定的图像。 I know that using import (import image from
my_image.jpg
) can allow me to import a specific image and then display it by using <img src={image} />
but here the application is dynamic and should be applied on different images every time depending on the path I enter in the input text.我知道使用导入(从
my_image.jpg
导入图像)可以让我导入特定图像,然后使用<img src={image} />
显示它,但这里的应用程序是动态的,应该应用在不同的图像上时间取决于我在输入文本中输入的路径。
Thanks in advance提前致谢
The reason is that the src property of the img component is not looking for a string path on disk.原因是 img 组件的 src 属性不是在磁盘上寻找字符串路径。 When you use the input tag type of file , you are getting back a location to a file but you're in need of a blob file object in memory.
当您使用file的输入标签类型时,您将返回文件的位置,但您需要 memory 中的 blob 文件 object。
So in order to display the image, you need to create a blob URI from that path and then set that to the img src property appropriately.因此,为了显示图像,您需要从该路径创建一个 blob URI,然后将其适当地设置为 img src 属性。
//inside your input onChange event...
var fileName = event.target.value; //<==== just the name
var fileBlob = URL.createObjectURL(event.target.files[0]); //<==== the actual file object
You can now set fileBlob to a piece of State and set您现在可以将 fileBlob 设置为一块 State 并设置
<img src={mystate} alt='something'/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.