1

i've been stuck on this one for a while and any help is appreciated. Images are stored as Base64 and i just can't get the setup right:

I have the following API in my ProductRestController

@GetMapping("/{id}/image")
public byte[] getProductImageByProductId(@PathVariable Long id) {
    return productFacade.getProductImageByProductId(id);
}

my product.service.ts:

  getProductImageByProductId(productId: number) {
    return this.http.get(ServiceConfig.serverBaseUrl + `/product/${productId}/image`, {responseType: 'arraybuffer'});
  } // I guess it has to be something with the responseType? I tried all types..

in my select-product.component.ts:

  getProductImage(product: Product) {
      this.productService.getProductImageByProductId(product.id).subscribe( base64 => {
        if(base64) {
          this.base64 = 'data:image/jpg;base64,' + base64; // this.base64 is a String
        }
        else {this.base64 = 'assets/no-image-icon.png';}
      });

and finally the html:

      <img [escape]="false"
           pTooltip='<img style="max-height: 100%; max-width: 100%" src="{{base64}}" >'
           tooltipPosition="right" (mouseover)="getProductImage(product)"
           src="assets/eye-icon.png" style="max-width: 100%; width: 80%; margin-bottom: 3px">

}

It should show an image in a tooltip when you hover on an icon, but whatever responseType i'm setting in my service, it will give me a parsing error with in the response some gibberish in the style of : %�S���cs5���&D�TdE£t6�U�e���u��F'���������������Vfv��������7GWgw��������5!1AQaq"2...

Thank you for your help!

2
  • provide base64 for testing Commented Aug 17, 2020 at 15:02
  • Hi @programoholic , here you go: 0bin.net/paste/… Commented Aug 17, 2020 at 15:10

2 Answers 2

1

You should use responstype blob. since you return byte and not base64 string.

Because this Java getmapping tells you "byte[]"

@GetMapping("/{id}/image")
public byte[] getProductImageByProductId(@PathVariable Long id) {
    return productFacade.getProductImageByProductId(id);
}
  getProductImageByProductId(productId: number): Observable<any> {
    return this.http.get<Observable<any>>(ServiceConfig.serverBaseUrl + `/product/${productId}/image`, {responseType: 'blob'});
  } /
   export class SelectProductComponent{

    constructor(private sanitizer:DOMSanitizer, /*other params*/){}


    public someMethod(product:Product):void{
    this.productService.getProductImageByProductId(product.id).subscribe( data =>{
         var reader = new FileReader();
         const blob = new Blob([data], {
            type: 'image/jpeg'
             });
         //EDIT: Don't use function. my bad
         reader.onloadend = () => {
           const untrustedData = reader.result; 
           this.base64 = this.sanitizer.bypassSecurityTrustUrl(untrustedData);          
         } 
         reader.readAsDataURL(blob); 
         
       }
 
    }

   
       // also you can use ObjectURL if you 
      //just want the image and don't need the conversion to base64

   
        public someOtherMethod(product:Product):void{
    this.productService.getProductImageByProductId(product.id).subscribe( data =>{
            const blob = new Blob([data], {
            type: 'image/jpeg'
             });
           const objectUrl = URL.createObjectURL(blob);
           this.image = this.sanitizer.bypassSecurityTrustUrl(objectURL);
         }
       }
 
    }

}

Sign up to request clarification or add additional context in comments.

3 Comments

Hi @Luxusproblem , thanks for helping.For your first method suggestion, i get : TS2339: Property 'base64' does not exist on type 'MSBaseReader'. and TS2339: Property 'sanitizer' does not exist on type 'MSBaseReader'. So i'm not sure what to do on that one. I'm trying out someOtherMethod(). as it seems to compile, i keep you updated
@h0tpot8o hey I saw that I used "function" this not the way I intended it. Sorry. Use the updated version. maybe this helps, since "function" messes with the "this." reference. Also you Put a Whole HTML statment into the tooltip. I fear this will not work with the URLs as you intend it. The whole statement HTML statement needs to be sanitzed I think. But you should try.
Hey @Luxusproblem, i got it fixed with Traycho's solution. I really appreciate your input though, thank you for helping ;)
0

What do you think about returning base64 encoded string as response ?

@GetMapping("/{id}/image")
public String getProductImageByProductId(@PathVariable Long id) {
    byte[] image = productFacade.getProductImageByProductId(id);
    return Base64.getEncoder().encodeToString(image);
}

Note: Is you have images as bas64 in the db, just return it as response.

1 Comment

This worked for me without changing anything front-end, simple and effective! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.