I want to get image from server in Angular 7 with blob. I wrote this service for send request:
downloadImage(id: number): Observable<any> {
console.log('in service ')
return this.http.request('GET', this.appConfig.apiEndpoint + '/Post/DownloadFileForManager/' + id, { responseType: 'arraybuffer' });
}
My Typescript code:
DownloadImage(id: number): Observable<any> {
this.postService.downloadImage(id).subscribe(data => {
const arrayBufferView = new Uint8Array(data);
const blob = new Blob([arrayBufferView], { type: 'image/jpeg' });
const urlCreator = window.URL;
const imageUrl = urlCreator.createObjectURL(blob);
this.image = this.sanitizer.bypassSecurityTrustUrl(imageUrl);
})
return this.image;
}
This is my HTML code:
<ng-container matColumnDef="thumbnail">
<th mat-header-cell *matHeaderCellDef> {{ 'GENERAL.PHOTO' | translate }} </th>
<td mat-cell *matCellDef="let element">
<img class="table-user-pic"
[src]="DownloadImage(9)"
id="photo"
alt="user avatar" width="50">
</td>
</ng-container>
now when i send the request it send many request to server .
i have 4 item but it send for firstImage 4 time and for next 8 time .
whats the problem ? how can i solve this problem ???