【问题标题】:show image in angular6 and AspCore with Blob以角度 6 和带有 Blob 的 Asp Core 显示图像
【发布时间】:2019-01-28 19:55:17
【问题描述】:

我想显示产品列表。此列表还包括产品照片。例如,包括产品名称、产品标题、照片 URL 等。 . .是。

export interface Product {
   id:number;
   productTitle:string;
   productName:string;
   color:string;
   productImageName:string;
   price:number;
   gurantyMonth:string;
   gurantyCompanyName:string;
   catId:number;
   values:Productdetail[];
}

然后我像这样向服务器发送请求:

  public GetProductList():Observable<Product[]>{
    return this.http.get<Product[]>(this.url+'GetProductList',{headers:this.headers})
          .pipe(
                 tap(() => this.log(``)),
                 catchError(this.handleError<any>(''))
              );
 }

这是Headers =>

const httpOptions = { headers: new HttpHeaders({ 'Content-Type': 'application/json; charset=utf-8' }) };

和我的服务器获取此地址:

file:///C:/Users/Mr-Programer/Downloads/585e4bcdcb11b227491c3396.png

我的图片是这个地址,它返回真实地址,但角度不显示图片,它说我unsafe

后端和前端在不同的环境中

有什么问题?如何解决这个问题并在 Angular 中显示图像?我如何在其中使用 Blob?

【问题讨论】:

  • 服务器在节点中?
  • @SachinShah 没有。 ASP 核心
  • 问题在于从服务器端上传图像。确保您以正确的路径上传图片。
  • @SachinShah 它上传图片并将图片放在当前路径中

标签: javascript angular typescript angular6


【解决方案1】:

您必须将 Url 解析为 SafeUrl。使用包DomSanitizer 获取方法bypassSecurityTrustUrl(value: string): SafeUrl。阅读它here

export class MyComponent {
  constructor(private domSanitizer: DomSanitizer) {}

  exampleFunction(imageUrl: string): SafeUrl {
    return this.domSanitizer.bypassSecurityTrustUrl(imageUrl);
  }
}

【讨论】:

  • 我如何使用Blob
  • 您是否将返回值包含在模板或 imageUrl 中作为字符串?
  • &lt;img src="{{data.productImageName}}"&gt;
  • 试试&lt;img [src]="exampleFunction(data.productImageName)"&gt;
  • 哦忘了return声明,请更正后再试。
猜你喜欢
  • 1970-01-01
  • 2011-09-28
  • 2012-04-02
  • 1970-01-01
  • 2022-01-26
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 2021-08-03
相关资源
最近更新 更多