Angular 5 - How to display image in HTML that comes as output from a http POST?

后端 未结 2 2047
心在旅途
心在旅途 2021-01-21 14:05

I need to display an thumbnail/image that comes from a POST request

Postman shows the output in right way

I\'m trying to use the same i

2条回答
  •  醉酒成梦
    2021-01-21 14:36

    In order to display the image in the DOM, we need to convert from blob to base64. Here is the code

    createImageFromBlob(image: Blob) {
       let reader = new FileReader();
       reader.addEventListener("load", () => {
          this.imageToShow = reader.result;
       }, false);
    
       if (image) {
          reader.readAsDataURL(image);
       }
    }
    

    Also, make sure you use DomSanitizer injectable service by applying safeurl to image src.

提交回复
热议问题