I'm working of Gallery feature of my website where I wanna show all images to client. I have successfully fetched images from server to client end in the form of blob. Images also display but on second attempt. I need to visit that gallery page for first time for fetching images then I need to visit any page and re-visit that gallery page for displaying those images.
Here is my code. Kindly let me know where I'm making mistake.
Code of main Gallery Component
constructor(props) {
super(props);
this.state = {
eventDetail: [],
images: [],
imagesInBlob: [],
url: ''
}
this.getAllEventsData = this.getAllEventsData.bind(this);
}
componentDidMount() {
this.getAllEventsData();
}
getAllEventsData() {
axios({
method: 'POST',
url: '/api/Account/GetAllEventsData',
contentType: 'application/json',
data: {},
}).then(function (response) {
console.log(response);
this.setState({
eventDetail: response.data
});
this.getAllImages();
}.bind(this))
}
getAllImages() {
axios({
method: 'POST',
url: '/api/Account/GetAllEventImages',
contentType: 'application/json',
data: {},
}).then(function (response) {
console.log(response);
this.setState({
images: response.data
});
this.getImageBlobs();
}.bind(this))
}
getImageBlobs() {
console.log("Image Objects:", this.state.images);
for (var i = 0; i < this.state.images.length; i++) {
this.fetchEventImage(this.state.images[i].image_path);
}
this.setState({
imagesInBlob: imageArr
});
console.log("Images in Blob:", this.state.imagesInBlob);
}
fetchEventImage(imagePath) {
var path = {
ImagePath: imagePath
}
axios({
method: 'POST',
url: '/api/ImageFetch/FetchEventImages',
responseType: 'blob',// important
headers: {
'Content-Type': 'application/json'
},
data: path
}).then(function (response) {
var re = /(?:\.([^.]+))?$/;
const url = window.URL.createObjectURL(new Blob([response.data]));
imageArr[img_arr_cnt++] = url;
console.log("URL: ", url);
}).catch(error => {
console.log("Status:", error.response.status);
console.log("Data:", error.response.data);
});
}
handleImageDisplay() {
var imgTag = "<img src=" + this.state.imagesInBlob[0] + '" />';
return imgTag;
}
render() {
return (
<>
<section id="single-project" className="section">
<div className="container row justify-content-center">
<div className="col-lg-12">
<div className="row justify-content-center">
<div className="col-lg-6 project-info">
<h3>Meeting on 17th May 2019</h3>
<DisplayComp propName={this.state.imagesInBlob[0]} />
<img src={this.state.imagesInBlob[0]} />
</div>
</div>
</div>
</div>
</section >
</>
);
}
DisplayComp component code
constructor(props){
super(props);
}
render(){
return (
<img src={this.props.propName} alt="image"/>
);
}
Please let me know where I'm making mistake and how can I display it on client end?