javascript - 在 ReactJS 中显示来自烧瓶 send_file 函数的图像
问题描述
我使用 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
在 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)
返回这个:
我需要稍后使用渲染图像,<img src={this.state.images}/>
但它不显示任何内容。有什么建议吗?
解决方案
你 dataImage.data 是二进制大对象,所以你应该尝试:
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);
}
推荐阅读
- outlook - 在 getMimeContent() 方法的情况下,您必须先加载或分配此属性,然后才能读取其值
- c# - 在 C# 中,当我单击第二种形式的按钮时,如何在第一种形式中打开第三种形式
- java - 没有为edittext显示键盘
- java - Java 文件正在从路径 /tmp/hsperfdata_*username*/ 中消失
- windows - 执行不打开cmd
- android - Android Retrofit Interceptor 为所有调用添加 body 参数
- android - 如何绘制属于画廊一部分的图像
- printing - 添加 CUPS 打印服务器过滤器
- angular - 带有路由器事件的页面标题在 Angular2 中返回相同的标题
- r - 有没有更好的方法来处理跨越反子午线(日期线)的 SpatialPolygons?