【问题标题】:Display image via Typescript model in Angular在 Angular 中通过 Typescript 模型显示图像
【发布时间】:2019-08-20 20:22:32
【问题描述】:

我的Angular (v8) 项目中有一个个人资料页面,通常我使用以下方法来显示用户照片:

<img class="profile-user-img" src="./DemoController/GetPhoto?Id={{rec.Id}}" />

但是,我认为还有另一种方法可以通过 model.ts 绑定图像,该图像已经从数据库中填充为其他字段。通过这样做,我只需向数据库发送一个请求而不是两次,并且照片数据将包含在model.ts 中。那么,我该怎么做呢?渲染前是否需要特殊的转换图像数据?

注意: 我使用byte[] 作为C# 端照片的数据类型。有什么想法吗?

这是我的模型。 ts如下:

export interface IUserProfile {
    Id: number;
    Photo: string | ArrayBuffer;
    NameSurname: string;
}

【问题讨论】:

  • 类似的问题还有很多:link

标签: c# asp.net-mvc angular typescript


【解决方案1】:

我认为您可以只使用字节数组来填充img.src 属性。 在您的组件模板中您可以编写如下:

<img class="profile-user-img" src="data:image/gif;base64,{{Profile.Photo}}" />

您在组件中的哪个位置:

@Component({})
export class User {
  Profile: UserProfile;
}

【讨论】:

  • 感谢回复,似乎是最快的方法,但我遇到了“GET unsafe:data:image/gif;base64, ... net::ERR_UNKNOWN_URL_SCHEME”
  • 为什么会报错。我认为在 html 中显示图像是一种正常的方法。 UserProfile 实例化时是否已经发生了 http 请求?
  • 好的,在您更新答案后现已修复。投票了,非常感谢;)
猜你喜欢
  • 1970-01-01
  • 2011-08-13
  • 2017-10-02
  • 2018-04-14
  • 2014-07-18
  • 2013-07-22
  • 2011-06-11
  • 1970-01-01
  • 2015-12-12
相关资源
最近更新 更多