How to get image from rest api using HttpClient in angular 4

点点圈 提交于 2019-12-24 22:07:00

问题


I have a simple cuestion. I have the following service in angular 4 app.

  getProviderPhoto(id:string){
    return this.http.get(this.apiUrl+"/"+id+"/images", {responseType: "blob"});
  }

I need to convert this Blob response to Image file to use the following method:

 createImageFromBlob(image: Blob) {
    let reader = new FileReader();
    this.photo = new File([image],"foto.png",{ type: 'image/png' });
    reader.readAsDataURL(this.photo);
    reader.onload = (event: any) => {
      this.image=event.target.result;
      console.log(this.image);
    }
 }

console.log

data:image/png;base64,ImlWQk9Sd.....

To use the downloaded image to show it in my html:

<div class="profile-image center" [ngStyle]="{ 'background-image': 'url(' + image + ')'}"></div>

On the other hand, if I do this, it works perfectly:

this.image="http://localhost:8083/v1/providers/"+this.id+"/images";

How can I get the same result but using Angular HttpClient.Get?

来源:https://stackoverflow.com/questions/50356471/how-to-get-image-from-rest-api-using-httpclient-in-angular-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!