【问题标题】:Displaying image returned with REST callAngular2 - 显示通过 REST 调用返回的图像
【发布时间】:2017-09-18 23:59:14
【问题描述】:

我想显示用户选择的项目的图像。当用户单击某个项目时,会进行休息调用并检索图像但我无法显示它。

我已尝试遵循 herehere 的想法,但似乎无法让它们发挥作用。

基本原则(据我所知)是,当其余调用响应(应该是)图像/png 类型时,我可以像这样在我的 TS 中格式化我的图像源:

import {DomSanitizer} from '@angular/platform-browser';
constructor(private _DomSanitizationService: DomSanitizer ){}

...

this.showPage = "data:image/png;base64," + rest_response;

然后使用 "DomSanitizer" 使其能够在 html 中使用,如下所示:

<img [src]="_DomSanitizationService.bypassSecurityTrustUrl(showPage )">

但是当我尝试使用它时,图像损坏了,当我检查它时,我看到:

src="data:image/png;base64,�PNG...,㘑��" etc...

任何和所有的帮助或解释为什么会受到赞赏。

【问题讨论】:

  • 你的rest后端是否在返回之前将图像编码为base64?

标签: image rest angular response render


【解决方案1】:

试试这个:

this.showPage = "data:image/png;base64," + rest_response;

然后很简单,

<img [src]="showPage" />

正如在这个 SO question 中回答的那样。

【讨论】:

  • 图像仍然损坏,但现在源数据以“不安全:”开头
  • 我看到其他人使用 DomSanitizationService 而不是 DomSanitizer,然后还导入 BROWSER_SANITIZATION_PROVIDERS 并将其列为提供程序。 More here.
猜你喜欢
  • 2012-11-28
  • 1970-01-01
  • 2019-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-26
相关资源
最近更新 更多