【发布时间】:2021-11-11 10:37:39
【问题描述】:
我正在尝试将字节数组绑定到角度的图像标签。 我知道字节数组是正确的,因为我可以下载它并从我的 API 中查看它。
我创建了这样的图像:
<img [src]="src" />
然后在我的代码中,我像这样清理字节数组:
this.src = this.sanitizer.bypassSecurityTrustResourceUrl(`data:image/svg+xml,${this.location.qrCode}`);
在我的控制台中我可以看到:
但是图片没有显示。我做错了什么?
我尝试了其他一些方法:
const reader = new FileReader();
reader.onload = (e) => (this.src = this.sanitizer.bypassSecurityTrustResourceUrl(e.target.result.toString()));
reader.readAsDataURL(new Blob([this.location.qrCode]));
和
this.src = this.sanitizer.bypassSecurityTrustResourceUrl(`data:image/svg+xml;base64,${this.test}`);
和
this.src= btoa(this.location.qrCode);
和
const reader = new FileReader();
reader.onload = (e) => (this.src = e.target.result);
reader.readAsDataURL(new Blob([this.location.qrCode]));
他们都没有工作:(
【问题讨论】:
-
这看起来像 base64 编码,但 URL 缺少 base64 关键字。
标签: angular svg angular-dom-sanitizer