我正在从Nodejs服务器接收二进制数据,现在我需要显示一张图片。
我该如何实现?是否有将bindata转换为JPEG或其他格式的方法?或者是在服务器上将其转换,然后将该图像发送到React?
以下是我尝试使用图像标签显示二进制数据(item.Image.data.data
)的方式:
<img src={item.Image.data.data} />
这是我的详细的React代码片段:
componentDidMount(){
let self = this;
axios.get('http://localhost:8080/list')
.then(function(data) {
console.log(data);
self.setState({post:data.data});
});
}
<ul className="w3-ul w3-card-4 w3-light-grey">
{ this.state.post.map((item, index) => {
return (
<Link to="/displaylist" style={{ textDecoration: 'none' }} key={index}>
<li className=" w3-hover-green w3-padding-16" onClick={this.handleClick(item.Id)}>
<img src={item.Image.data.data} className="w3-left w3-circle w3-margin-right" width="60px" height="40px" />
<span>{item.Firstname}</span><br/><br/>
</li>
</Link>
)}
)}
</ul>
这是我的Node.js代码片段:
server.get('/list', function(req, res) {
databaseInterface.listStudent(function(err, students) {
var myJSON = students;
res.json(myJSON);
// You should see the newly saved student here
});
});
<img src="data:image/jpeg;base64,{binary data}" />
我认为这应该可以。 - Jacob Goh