25

I am trying to get a blob response from a request, and then generate a URL from that blob and set this URL as an image's source.

But the image is not loading.

This is my HTML:

<img src="{{previewSignsrc}}" alt="Sign Thumbnail">

And this is my .ts file:

this.signModalDataService.getPreviewSignature({'name': this.SignatureName, 'font_type': 0});
    this.signModalDataService.previewSignature
      .subscribe((res) => {
        console.log(res);
        let blob = new Blob([res['_body']], {type: 'image/png'});
        this.previewSignsrc = URL.createObjectURL(blob);
        this.showPreviewSign = true;
      });

I used same method to set url for ng2-pdfviewer and it worked fine.

Matt C
  • 4,470
  • 5
  • 26
  • 44
Shoaib Iqbal
  • 2,420
  • 4
  • 26
  • 51

6 Answers6

51

You can dislay image like this code

this.config.getData()
          .subscribe((blob : any) => {
            let objectURL = URL.createObjectURL(blob);       
            this.image = this.sanitizer.bypassSecurityTrustUrl(objectURL);

          });

If your data is base64 display like this

 this.config.getData()
      .subscribe((baseImage : any) => {
        let objectURL = 'data:image/jpeg;base64,' + baseImage.image;
         this.thumbnail = this.sanitizer.bypassSecurityTrustUrl(objectURL);

      });

Demo https://stackblitz.com/edit/display-image-from-api

Hien Nguyen
  • 24,551
  • 7
  • 52
  • 62
4

As was metioned earlier by Memo 313 MediaSA, FileReader works.

 const reader = new FileReader();
 reader.readAsDataURL(data); //FileStream response from .NET core backend
 reader.onload = _event => {
     url = reader.result; //url declared earlier
     image.nativeElement.src = url; //image declared earlier
 };
3

You can use new FileReader(); I tried so much codes and that's the only one that worked for me.

  var reader = new FileReader ();
 reader.readAsDataURL(response) <= from inner . subscribe
  reader.onload = (_event) => {
  this.retrieveURL = reader.result;
 }

 .html
 [src]="retrieve URL" 

Bear with me I typed from my cellphone

That's all no need to use sanitizers, hope it helps somebody out there, ooh I am using Angular8

2

this code is the best for blob(for example asp file stream in backend) and 100% work.

.ts:

 image: any;
  constructor( 
    private sanitizer: DomSanitizer,
    ) { }
 this.signModalDataService.previewSignature
    .subscribe(blob => {
        let objectURL = URL.createObjectURL(blob);
        this.image = this.sanitizer.bypassSecurityTrustUrl(objectURL);
      })

.html

<img [src]="image">
Mohammad Naghsh
  • 207
  • 3
  • 10
1

If you use JsonConvert from a .Net API to return an object in which one of the fields is the image byte[] it will convert it to a base64 string. Then you don't need anything special when calling the api or displaying the image.

1

This works for me:

this.httpClient.get(Endpoints.PRODUCTS + '/pictures/download', {'params': queryParams, responseType: 'blob'})
  .subscribe(
    value => {
      // Add Preview
      const reader = new FileReader();
      reader.readAsBinaryString(value);
      const addPreview = (fileBase64) => {
        this.urls.push(`data:${value.type};base64,${btoa(fileBase64)}`);
      };
      reader.onload = function (e) {
        addPreview(e.target.result);
      };
    },
    (error) => {
      // Refactor
      // this.errorList = error.error.data.errorList;
      console.log(error);
    })
;
Daniel Rch.
  • 150
  • 1
  • 7