I want to show a list of photos using a blob, but it is not displaying any of the images. I created and provided a link to an example in stackblitz. What have I missed that can be used to resolve this issue?
I'm using this code for downloading an image :
getImage(imageUrl: string): Observable<Blob> {
return this.http.get(imageUrl, { responseType: 'blob' });
}
and retrieve the images using a service:
CreateImageBlob(imageUrl: string): void {
this.productService.getImage(imageUrl).subscribe(
(val)=>{
this.getImageFromService(val)
},
response => {
console.log("POST in error", response);
},
() => {
console.log("POST observable is now completed.");
});
}
Using this to create a new file:
public getImageFromService(image:Blob){
let reader=new FileReader();
reader.addEventListener("load",()=>{
this.imageBlobUrl=reader.result;
},false);
if(image) {
reader.readAsDataURL(image)
}
}
and finally the template for displaying the image looks like:
getImageFromService. It could be rewritten togetImageFromService(image:Blob) { this.imageBlobURL = new URL.createObjectURL(image); }. This may or may not fix your problem, but will anyway make it simpler and use less memory, as a rule of thumb, you don't need readAsDataURL unless you know you do.