简体   繁体   English

从 ReactJS 中的 flask send_file function 显示图像

[英]Display image from flask send_file function in ReactJS

I use Flask send_file to send an image to the client like this:我使用 Flask send_file 将图像发送到客户端,如下所示:

@app.route('/get-cut-image',methods=["GET"])
def get_cut_img():
   response = make_response(send_file(file_path,mimetype='image/png'))
   response.headers['Content-Transfer-Encoding']='base64'
   return response 

and in React I use axios to read the request在 React 中我使用 axios 来读取请求

try {
  const dataImage = await axios.get(
    "http://localhost:5000/get-cut-image"
  );
  this.setState({
      images: dataImage.data,
    });
  console.log(dataImage.data);
} catch (error) {
  console.log(error);
}

console.log(dataImage.data) return this: console.log(dataImage.data)返回: 在此处输入图像描述

I need to render the image later using <img src={this.state.images}/> but it does not display anything.我需要稍后使用<img src={this.state.images}/>渲染图像,但它不显示任何内容。 Any advice?有什么建议吗?

You dataImage.data is Binary large object, so you should try:你dataImage.data是二进制大object,所以你应该试试:

 try { const dataImage = await axios.get( "http://localhost:5000/get-cut-image" ); const blob = await dataImage.data.blob() const url = URL.createObjectURL(blob) this.setState({ images: url, }); console.log(url); } catch (error) { console.log(error); }

The image sent from the flask app can be simply displayed by <img src={"http://localhost:5000/get-cut-image"}/> .从 flask 应用程序发送的图像可以通过<img src={"http://localhost:5000/get-cut-image"}/>简单地显示。

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

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