gpt4 book ai didi

javascript - 将 blob 响应视为 Angular 图像

转载 作者:太空狗 更新时间:2023-10-29 17:33:21 31 4
gpt4 key购买 nike

我正在尝试从请求中获取 blob 响应,然后从该 blob 生成一个 URL 并将该 URL 设置为图像的来源。

但是图片没有加载。

这是我的 HTML:

<img src="{{previewSignsrc}}" alt="Sign Thumbnail">

这是我的 .ts 文件:

this.signModalDataService.getPreviewSignature({'name': this.SignatureName, 'font_type': 0});
this.signModalDataService.previewSignature
.subscribe((res) => {
console.log(res);
let blob = new Blob([res['_body']], {type: 'image/png'});
this.previewSignsrc = URL.createObjectURL(blob);
this.showPreviewSign = true;
});

我使用相同的方法为 ng2-pdfviewer 设置了 url,它工作正常。

最佳答案

你可以像这样显示图片

this.config.getData()
.subscribe((blob : any) => {
let objectURL = URL.createObjectURL(blob);
this.image = this.sanitizer.bypassSecurityTrustUrl(objectURL);

});

如果你的数据是base64显示这样

 this.config.getData()
.subscribe((baseImage : any) => {
let objectURL = 'data:image/jpeg;base64,' + baseImage.image;
this.thumbnail = this.sanitizer.bypassSecurityTrustUrl(objectURL);

});

演示 https://stackblitz.com/edit/display-image-from-api

关于javascript - 将 blob 响应视为 Angular 图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55591871/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com