我正在尝试显示从服务器端发送的图像,服务器发送图像然后将其转换为 blob 但我无法将其转换为图像以在 DOM 中显示
这是component.ts
中的代码
getImageFromService() {
this.authService.getProfileImage().subscribe(data => {
this.createImageFromBlob(data);
console.log(data);
}, error => {
console.log(error);
});
}
此代码以 blob 形式从服务器获取图像
这是我编写的将 blob 转换为图像的代码
createImageFromBlob(image: Blob) {
let reader = new FileReader();
reader.addEventListener("load", () => {
this.imageToShow = reader.result;
}, false);
if (image) {
reader.readAsDataURL(image);
}
}
这是HTML中的代码
<img [src]="imageToShow " alt="" class="img">
这是 chrome devtool 中的响应
Blob(763750) {size: 763750, type: "text/xml"}
最佳答案
我认为您只需执行此操作即可将 Blob 转换为文件:
const file = new File([blob], fileName);
https://stackoverflow.com/questions/50697397/
相关文章:
python-3.x - 在 flask 应用程序中设置 python dash 仪表板
angular - 以 Angular 注入(inject)具有基类类型的父组件
json - 如何用 JSON 表示 PostgreSQL 中的稀疏 RDBS 列
angular - Material 自动完成 : Link multiple text input
amazon-web-services - 检查 adminCreateUser 在 AWS Cog
google-chrome - Chrome 不尊重主机文件条目