【问题标题】:Angular: Display image from blobAngular:显示来自blob的图像
【发布时间】:2018-01-25 20:42:30
【问题描述】:

无法显示我的图像。知道我做错了什么吗?

 <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')});
          })
      }

数据从服务中返回

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

【问题讨论】:

    标签: angular


    【解决方案1】:

    如果你有blob图像值,你可以直接在图像标签中设置值..

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

    我已经在 angular7 中尝试过,它的工作原理..

    【讨论】:

      【解决方案2】:

      您需要为 blob 创建一个对象 URL:

      this.imageToShow = URL.createObjectURL(res);
      

      更新

      另外,如果您将responseType 设置为blob,那么您将返回一个blob,因此您可以完全删除getImageFromService 中的map 语句

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-05-27
      • 2014-07-05
      • 2013-06-12
      • 2016-11-18
      • 1970-01-01
      • 2013-12-31
      • 2014-03-19
      相关资源
      最近更新 更多