angular - 使用 DomSanitizer 后拒绝加载图像
问题描述
我有一项从服务器加载图像的服务,我想在<img [src]="imageURL">
标签中显示该图像。我使用DomSanitizer
使该 URL 安全,但我仍然遇到不安全的错误并且图像未显示。
在我的file.service.ts我有这个功能:
downloadResource(downloadUrl: string): Observable<Blob> {
return this.httpClient.get<Blob>(downloadUrl, {
headers: new HttpHeaders({
'accept': 'application/octet-stream',
'content-type': 'application/json'}),
responseType: 'blob' as 'json'
});
}
该功能适用于下载图像和其他文件。
然后,在我的image.component.ts我这样调用函数:
imageURL: any;
constructor(private fileService: FileService, private domSanitizer: DomSanitizer ) { }
ngOnInit() {
this.fileService.downloadResource(downloadUrl)
.subscribe(data => {
const blob = new Blob([data], {type: 'application/octet-stream'});
this.imageURL = this.domSanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(blob));
});
}
我的image.component.html看起来像这样:
<img [src]="imageURL">
我收到以下错误:
拒绝加载图像“blob:https://someurl.72726 ”,因为它违反了以下内容安全策略指令:“img-src * data:”。
我也试过了:bypassSecurityTrustResourceUrl
但我遇到了同样的错误。
我错过了什么吗?
编辑:当我imageURL
在控制台上打印时,我得到了这个:
SafeValue 必须使用 [property]=binding: blob: https://someurl.72726(参见http://g.co/ng/security#xss)
解决方案
推荐阅读
- tensorflow - 试图理解 tensorflow 数据集中小批量中的随机播放
- java - 如何使用 JOptionPane 删除 ArrayList 中的元素?
- javascript - 我可以在 React 中使用 switch 语句来管理我的表单吗?
- python - 删除边界线上方的图像顶部以检测文本文档
- javascript - 从单独的链接更改部分 href 不适用于从选择下拉列表中进行的更改
- javascript - react-native-video:无法读取 null 的属性“常量”
- java - 为什么我的底部导航栏不能显示?
- javascript - 将空数组作为第二个参数传递时,React Hook useEffect 缺少依赖项
- android - Flutter 升级到 v1.14.7-pre.83 并迁移到 AndroidX 后只显示白屏
- java - 将字符串从数组列表转换为另一个字符串