【发布时间】:2018-05-16 05:40:49
【问题描述】:
我正在尝试设置图像。当我设置第一张图像时它工作正常,但是当我尝试上传第二张图像时,它会创建第二个图像框,但不会在其上显示任何内容,而是将其显示在第一个图像框的顶部。我希望它们彼此分开。
我尝试通过在showImage函数中返回imgUrl(返回imgUrl)直接使用blob,但它也没有在img src中设置blob。 收到图片(URL)后,如何循环并在每个框中分别设置所有图像?
class Card extends Component {
constructor({props, pic, token}) {
super(props, pic, token);
this.state = {
pic: pic
};
readResponseAsBlob(response) {
return response.blob();
}
validateResponse(response) {
if (!response.ok) {
throw Error(response.statusText);
}
return response;
}
logError(error) {
console.log('Looks like there was a problem: \n', error);
}
showImage(responseAsBlob) {
var container = document.getElementById('images');
var imgElem = container.getElementsByTagName('img')[0];
var imgUrl = URL.createObjectURL(responseAsBlob);
imgElem.src = imgUrl;
console.log(imgUrl);
}
urlFetch(data) {
fetch(data, {
headers: new Headers({
'authorization': `Bearer ${this.props.token}`,
'Content-Type': 'application/json'
})
})
.then(this.validateResponse)
.then(this.readResponseAsBlob)
.then(this.showImage)
.catch(this.logError);
}
render() {
const { pic } = this.state;
return (
<a style={{width: 200, height: 250}} className='tc dib br3 ma2 pa2 grow bw2 shadow-5'>
<div id='images'>
<img style={{width: 175, height: 175}} className='tc myimage br3' alt='none' src={ this.urlFetch(pic) }/>
</div>
</a>
);
}
}
【问题讨论】:
-
对不起,我可能是盲人,但我没有看到任何用于生成图像框的循环。是在另一个班级吗?如果是的话,你能展示一下吗?
-
@LeoOdishvili 图像框是在从另一个组件接收到包含 URL 的“图片”时创建的。
标签: javascript html reactjs