15

I have an api that returns a byte[], i want to display it as an image in my front application, so i used data url to display the image

this.cardBackgroundService.getImage(event.data.entitlementCertificateNumber, "C003").subscribe(data => {
  this.image = data;
  console.log(this.image);
});

<img src="data:image/png;base64,{{image}}"/>

the problem is when i display the response of the api in the console, it has this format and it doesn't display as an image

�PNG �Ou���j�000000000H��a��a````````��a��a��a```````��a��a��a````````��a��a��a```````��a��a````````�.r�����X��V��QS��\�ۂ���F�`�{lhXnJU��s��iiǯ�O1�;������

Aymen Kanzari
  • 1,765
  • 7
  • 41
  • 73

2 Answers2

13

You can display like this, it worked with me.

Import import { DomSanitizer } from '@angular/platform-browser';

Add DomSanitizer to constructor(private sanitizer: DomSanitizer) { }

this.cardBackgroundService.getImage(event.data.entitlementCertificateNumber, "C003")
.subscribe(data => {

  let objectURL = 'data:image/png;base64,' + data;
  this.image = this.sanitizer.bypassSecurityTrustUrl(objectURL);
});

In HTML

<img [src]='image' />
Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
  • 1
    i have this error: `data:image/png;base64,%EF%BF%BDPNG%0D%0A%1A%0A%00%00%00%0DIHDR%00%00%034%00%00%01%EF%BF%BD%08%02%00%00%00r@%EF%BF%BD%EF%BF%BD%00%00%EF%BF%BD%00IDATx%EF%BF%BD%EF%BF%BD%05XU%EF%BF%BD%EF%BF%BD%EF%BF%BD%EF%BF%BD?%EF%BF%BDw%EF%BF%BD%1B%EF%BF%BD%EF%BF%BDw%EF%BF%BDV@%11%0B%0B%EF%BF%BD.% net::ERR_INVALID_URL` – Aymen Kanzari May 03 '19 at 10:53
  • 1
    you need check your image content, you can see the demo at https://stackblitz.com/edit/display-image-from-api – Hien Nguyen May 03 '19 at 10:53
  • what is the typescript service type for byte? https://stackoverflow.com/questions/74467236/react-typescript-api-type-for-java-byte-image-png – mattsmith5 Nov 16 '22 at 21:30
  • This is Perfect example, @HienNguyen , thanx for your vital post on stackblitz :) Happy to fix on my side, some folks were mention to append URL, dont know how it works t=in there case, but for me it is huge headache of ALB from AWS, but this way great working for me. thnx again. – Dnyaneshwar Jadhav Apr 02 '23 at 13:37
  • @HienNguyen I need to know whats wrong here, can you please help me here as well ? https://stackoverflow.com/questions/75912311/how-to-render-image-from-backend-server-as-byte-array-to-html-div-style-backgrou – Dnyaneshwar Jadhav Apr 02 '23 at 13:37
11

You need to convert your image data to a dataURL:

const reader = new FileReader();
reader.onload = (e) => this.image = e.target.result;
reader.readAsDataURL(new Blob([data]));

And in your component:

<img [src]="image"/>

Make sure you set the responseType to be of type 'blob' in your getImage() http get request.

Poul Kruijt
  • 69,713
  • 12
  • 145
  • 149
  • So what type of image from AWS should I set in my Spring Boot image field? byte[]? I want to send from backend to the frontend ResponseObject with some fields and also with image field... – Matley Oct 17 '20 at 23:54
  • what is the typescript service type for byte? https://stackoverflow.com/questions/74467236/react-typescript-api-type-for-java-byte-image-png – mattsmith5 Nov 16 '22 at 21:28