我正在尝试从沙发床中获取二进制数据,并通过socket.io将其发送到客户端。
我正在使用nano(https://www.npmjs.com/package/nano)。我可以获取附件,并可以通过socket.io将其发送给客户端。将二进制数据转换为URL后,URL始终显示空白图像。
const nano = require("nano")('http://username:password@my.couchDB.com');
const users = nano.db.use("users");
const io = require("socket.io")(8888);
io.on('connection', socket =>{
socket.on('getProfile', async data => {
try{
let f = await users.get(data.name);
let ob = Object.assign({}, f);
delete ob._id;
delete ob._rev;
let attachment = await users.attachment.get(ob.image,ob.image);
ob.imageData = attachment;
socket.emit('profile', ob);
}
catch(err){
socket.emit('err', err);
}
});
});
import React, { Component } from 'react';
import socketIOClient from "socket.io-client";
import Spinner from '../Spinner';
export default class test extends Component {
constructor(props){
super(props);
this.state = {
loading: true
};
}
componentDidMount(){
const socket = socketIOClient("http://10.20.30.40:8888");
socket.emit('getProfile', {name: 'abc', email: 'abc@xyz.com'});
socket.on('profile', (data) => {
let d= [];
d.push(data.imageData);
let ImgName = data.image;
let type = ImgName.split('.')[1];
let imageURL = window.URL.createObjectURL(new Blob(d, {type: 'image/' + type}));
this.setState({
url: imageURL,
loading: false
});
});
}
render() {
if(this.state.loading){
return (
<Spinner/>
);
}
else {
return (
<img src={this.state.url} alt="blah"/>
);
}
}
}
未渲染图像。我得到的只是一张破损的图像以及替代文字。帮帮我!