0

//Pipe code where we wil manage base64
import { HttpClient } from '@angular/common/http';
import { Pipe, PipeTransform } from '@angular/core';
import { ItemMaster } from '@cust-custap/core/http/item-master/item-master.service';
import { DomSanitizer } from '@angular/platform-browser';

@Pipe({
  name: 'getImageUrl',
})
export class GetImageUrlPipe implements PipeTransform {
  constructor(
      private httpClient: HttpClient,
      private itemMasterService :ItemMaster,
      public sanitizer:DomSanitizer ) {}

  transform(value: any, id: any): any {
      console.log(id);
      return this.itemMasterService.getItemImage(id).subscribe((data:any)=>{
        let img=this.sanitizer.bypassSecurityTrustResourceUrl('data:image/png;base64,'+data);
        return img;
    });
  }
}
<!-- HTML part-->
<div *ngFor="let product_catalog  of result; let i= index;"> 
  <img class="card-img img-fluid" width="75" height="75" alt=""
     [src]="'' | getImageUrl: product_catalog.ID">
     </div>
enter image description here

I want to show base64 image in html and i have used pipe for showing base64 image,also pipe is working correctly but image showing object-object in console,i have already used this.sanitizer.bypassSecurityTrustResourceUrl but its not working.please check below my code and let me what i am wrong here?

1 Answer 1

1

You're seeing [object object] because the pipe is returning a subscription, not the base64 string.

You need to use the AsyncPipe here.

Instead of returning a subscription, you can return an observable from your pipe's transform() method.

transform(value: any): Observable {
  return this.itemMasterService.getItemImage(value)
             .map(data => this.sanitizer.bypassSecurityTrustResourceUrl('data:image/png;base64,' + data));
}

And the HTML part could be like this

<div *ngFor="let product_catalog  of result; let i= index;"> 
  <img class="card-img img-fluid" width="75" height="75" alt=""
     [src]="product_catalog.ID | getImageUrl | async">
</div>
4
  • sir i have tried above code and now [object object] not showing but also image not render again.its showing---ng-reflect-src="data:image/png;base64,Qk1a9AAA" this check my updated fiddle where i have update the image
    – Kapil Soni
    Commented Oct 28, 2021 at 4:22
  • 1
    finally i have fixed above isue also after modified getItemImage function -this.sanitizer.bypassSecurityTrustResourceUrl('data:image/png;base64,'+data)
    – Kapil Soni
    Commented Oct 28, 2021 at 8:46
  • @KapilSoni I have updated my answer also. Commented Oct 28, 2021 at 9:41
  • can you tell me how to show spinner on image tag if image not loaded from API i have used appLazyload directive but its not working on that case?
    – Kapil Soni
    Commented Oct 29, 2021 at 6:37

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.