I am trying to return an image using a blob
request in Angular and display it in the HTML.
I have implemented the following code:
<img [src]="ImageUrl"/>
This is the TypeScript code I am using:
private src$ = new BehaviorSubject(this.Url);
dataUrl$ = this.src$.subscribe(url => {
return this.imageService.GetImage(url, this.id).subscribe(data => {
this.ImageUrl=data.changingThisBreaksApplicationSecurity;
return data.changingThisBreaksApplicationSecurity
})
});
Here is my service implementation:
GetImage(url, id): Observable<any> {
return this.httpClient
.get(this.appConfig.apiEndpoint+ url + id, { responseType: 'blob' })
.pipe(
map(res => {
if (res) {
return this.domSanitizer.bypassSecurityTrustUrl(URL.createObjectURL(res));
}
return null;
})
)
}
However, when I try to display the image in the HTML, it uses the following URL:
<img src="unsafe:blob:http://localhost:4200/6868babb-5ab2-491f-b519-2f2a3d0ed351">
But no image is displayed. What could be causing this issue? How can I resolve it?