admin管理员组文章数量:1278881
I have PNG image as ReadableStream and I need to display html element img with this ReadableStream as source. Should I convert it to something? Or how can I do it?
Thank you for answer
I have PNG image as ReadableStream and I need to display html element img with this ReadableStream as source. Should I convert it to something? Or how can I do it?
Thank you for answer
Share Improve this question asked Sep 1, 2017 at 14:11 SpookSpook 3733 gold badges6 silver badges16 bronze badges 1- you can convert png image to base64 and you can use it in img element and display. – Kumar Commented Sep 1, 2017 at 14:18
1 Answer
Reset to default 7From Google's docs:
- Get response's
blob
- Resolve its
Promise
- Create a local source with
URL.createObjectURL
- Add it to your document (or update state)
Here's an example
import React, { Component } from 'react'
function validateResponse(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
export default class ponentName extends Component {
state = {
src: null
}
ponentDidMount() {
fetch(`http://localhost:5000/api/images/home`)
.then(validateResponse)
.then(response => response.blob())
.then(blob => {
this.setState({ src: URL.createObjectURL(blob) })
})
}
render() {
return (
<div>
{ this.state.src && <img alt="home" src={ this.state.src }></img> }
</div>
)
}
}
In my case, data es from a send_file
response from a Flask
Python backend
本文标签: htmlJavascriptReactJSdisplay image with ReadableStream as sourceStack Overflow
版权声明:本文标题:html - Javascript - ReactJS - display image with ReadableStream as source - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741210136a2358947.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论