Angular 角度-从API渲染图像/jpeg数据
我的http呼叫:Angular 角度-从API渲染图像/jpeg数据,angular,Angular,我的http呼叫: 返回http.get(url.map)(res=>res.text()) 组成部分: httpService.getCustomBanner('path').subscribe( data => { this.image = 'data:image/jpeg;base64,'+ data }) HTML: <div *ngIf = "image"> <img [src]="sanitizer.bypassSecurityTru
返回http.get(url.map)(res=>res.text())代码>
组成部分:
httpService.getCustomBanner('path').subscribe(
data => {
this.image = 'data:image/jpeg;base64,'+ data
})
HTML:
<div *ngIf = "image">
<img [src]="sanitizer.bypassSecurityTrustUrl(image)"/>
</div>
我没有收到任何错误,API返回一种类型的图像/jpeg,
但什么都不是
编辑:取出数据:image/jpeg;base64
和dee zg的答案是解决问题的方法 在http调用中,您需要这样的内容(未经测试,从头部写入):
这将返回一个DomString
,您可以将它输入到您的img源中。在您的http调用中,您需要这样的内容(未测试,从头部写入):
这将返回一个DomString
,您可以将其输入img源。尝试使用以下命令
在组件中定义一个函数
import {DomSanitizer} from '@angular/platform-browser';
...
constructor(private sanitizer:DomSanitizer){}
sanity(url)
{
return this.sanitizer.bypassSecurityTrustUrl(url)
}
在模板中,您可以使用
<div *ngIf = "image">
<img [src]="sanity(image)"/>
</div>
尝试使用以下方法
在组件中定义一个函数
import {DomSanitizer} from '@angular/platform-browser';
...
constructor(private sanitizer:DomSanitizer){}
sanity(url)
{
return this.sanitizer.bypassSecurityTrustUrl(url)
}
在模板中,您可以使用
<div *ngIf = "image">
<img [src]="sanity(image)"/>
</div>
您好,我不确定[src],但您可以使用以下代码并测试它是否适合您
<div *ngIf = "image">
<img src="{{sanitizer.bypassSecurityTrustUrl(image)}}"/>
</div>
您好,我不确定[src],但您可以使用以下代码并测试它是否适合您
<div *ngIf = "image">
<img src="{{sanitizer.bypassSecurityTrustUrl(image)}}"/>
</div>
谢谢。我尝试了很多帖子,但这一篇对我来说很有用。对于其他人,我将在这里发布我的经验。对于我的测试,我的asp.net core API返回一个学生列表,其中包含byte[]数据类型图像,该图像以角度映射到Blob。
此外,类中的角度字段名称必须以小写开头以供引用,而不管其声明如何。例如
Student.Photo; // is declared in a Student class.
students: Student[];
//Call an api and put the result into students.
students[0].photo worked. But students[0].Photo caused an error of 'undefined'.
有关多个图像的工作示例,请参见以下代码
--角度组件HTML-----
终于对我有用了。大多数示例仅适用于单个图像,这与上面的示例一样非常容易实现。快乐编码 谢谢。我尝试了很多帖子,但这一篇对我来说很有用。对于其他人,我将在这里发布我的经验。对于我的测试,我的asp.net core API返回一个学生列表,其中包含byte[]数据类型图像,该图像以角度映射到Blob。
此外,类中的角度字段名称必须以小写开头以供引用,而不管其声明如何。例如
Student.Photo; // is declared in a Student class.
students: Student[];
//Call an api and put the result into students.
students[0].photo worked. But students[0].Photo caused an error of 'undefined'.
有关多个图像的工作示例,请参见以下代码
--角度组件HTML-----
终于对我有用了。大多数示例仅适用于单个图像,这与上面的示例一样非常容易实现。快乐编码 Myimage
变量现在似乎已填充,但我得到的是通用图像图标。Myimage
变量现在似乎已填充,但我得到的是通用图像图标。imageGotcha的url。不过,imageGotcha的.url没有任何变化。但一切都没有改变。