1

having trouble getting my image to show.. Any idea what i am doing wrong?

 <img style="width:175px" [src]="imageToShow">


getImage(){
  this.mccProgramService.getImageFromService().subscribe(
    (res) => {
      console.log(res);
      this.imageToShow = res;
    //  window.open(fileURL);
    }
  );
}

getImageFromService() {
        return this._httpClient.get("http://localhost:9080/mccr/api/file/download.action?contentFileId=11", {observe: 'response', responseType: 'blob'})
          .map((res) => {
            return new Blob([res.body], {type: res.headers.get('Content-Type')});
          })
      }

Data is coming back as this from service

<<other headers>>
<<Content-Type header>>
<<other headers>>
<<file binary data>>

3 Answers 3

1

If you have blob image value, you can directly set the value in image tag..

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

I have tried it in angular7, its working..

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

Comments

0

You need to create an object URL for the blob:

this.imageToShow = URL.createObjectURL(res);

UPDATE

Also, if you set the responseType to blob then you will get back a blob so you can remove the map statement entirely in getImageFromService

2 Comments

Added another suggestion
WARNING: sanitizing unsafe URL value blob:localhost:4200/d43b3a76-cbc0-45f5-97a2-48a9ef50751f (see g.co/ng/security#xss)
0

Your .ts file -

YourMethod().subscribe((blob : any) => { this.blobdata = blob.fileContents; }

Your .html file -

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

Your c# endpoint should return Ok(File(model.Byte, "image/jpeg"));

I hope it helps. And thank you, Deepa, your suggestion worked for me.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.