我正試圖將一個位元組陣列系結到angular中的一個圖片標簽上。 我知道這個位元組陣列是正確的,因為我可以下載它并從我的API中查看它。
我創建了一個像這樣的圖片:
我創建了一個像這樣的圖片。
<img [src] ="src" /> //span>
然后在我的代碼中,我對位元組陣列進行了這樣的處理:
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] )。
他們都沒有作業:(
)uj5u.com熱心網友回復:
我做了一些研究,結果發現我的API base 64編碼的位元組陣列,這就是為什么它不作業。 我簡單地用以下方式進行了解碼:
this.svgData = atob(this.location.qrCode) 。
然后能夠安全地呈現HTML:
this.svg = this.sanitizer.bypassSecurityTrustHtml(this.svgData) 。
我可以在我的模板中這樣使用:
< div [innerHtml]="svg"></div>
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/331120.html
標籤:
