【问题标题】:Convert Blob to image url and use in image src to display image将 Blob 转换为图像 url 并在图像 src 中使用以显示图像
【发布时间】:2018-12-03 19:01:30
【问题描述】:

如何将 blob 转换为 base64/图像?
我通过 API 调用获取此 blob,并尝试将其显示在
图片。我已经尝试过使用 stackoverflow 的答案,但没有任何帮助
我刚试过这个。

//Angular 5 code  
imageSrc;//global variable  
data = [{"image_blob":{"type":"img/jpg","data":[123,125]}}];//from api  
var blob1 = new Blob([new Uint8Array(data[0].image_blob.data)]);  
const imageUrl = URL.createObjectURL(blob1);  
console.log(imageUrl);//blob:http://localhost:8100/c489.etc  
this.imageSrc = imageUrl;  

<!-- html code -->  
<img [src]='imageSrc' alt="image">  

错过了什么。请推荐

【问题讨论】:

  • Convert blob to base64的可能重复
  • 不,我也尝试过使用该代码......但没有运气。在尝试了 7 天后发布了这个问题。我也消除了角度卫生问题。为了简单起见,在这里提一下。
  • 您的主要问题是什么?获取 base64 还是将其放入 html 中?
  • 放入html,无论如何我已经转换为imageurl。但这不起作用

标签: angular typescript blob azure-blob-storage filereader


【解决方案1】:

生成base64字符串:

var reader = new FileReader();
reader.readAsDataURL(blob); 
reader.onloadend = function() {
   base64data = reader.result;     
}

一旦你有了它,使用 Angular sanitizer 生成你要放入图像src 的字符串。示例:

import { DomSanitizer } from '@angular/platform-browser';
constructor( ... private sanitizer : DomSanitizer ...){}

public myFunction() : void {
    let mySrc = this.sanitizer.bypassSecurityTrustUrl('data:image/png;base64,' + base64data);
}

您可以根据需要修改'data:image/png;base64,'的类型。

最后,将其放入您的图像中:

<img [src]="mySrc" />

【讨论】:

  • powkachu 谢谢你,但尝试了上述所有方法。事实上,我已经创建了用于消毒的管道。但没有任何效果。
  • 哦,真的,这很奇怪...图像什么都没有显示?你检查你的base64字符串是否生成良好?
【解决方案2】:

当尝试从服务器响应中将图像加载为 blob 时,我遇到了 Angular2 (7) 认为提供的 url 不安全的问题。在寻找解决方案时,广泛建议的解决方案是使用DomSanitizer 绕过安全性。

在此处查看来自@Amirreza 的示例,但在其他 StackOverflow 帖子中也建议使用相同的解决方案。

the angular documentation page for DomSanitizer 上,他们写道:

绕过SecurityTrustUrl()

警告:使用不受信任的用户数据调用此方法会使您的应用程序面临 XSS 安全风险!

在我看来这样做是不安全的,除非你真的很确定图像源是可以信任的!
您应该考虑到,即使源来自您自己的服务器,它也可能是由用户上传的,并且有可能通过上传恶意图像来利用这种解决方案。

将 blob(图像)转换为 data-url(base64 字符串)并将其设置为图像元素的 src 会更好、更安全。

Data URLs are generally considered safe (MDN):

将数据编码为 base64 格式

base64 字符串通常是 url 安全的,这就是为什么它们可用于对数据 URL 中的数据进行编码。

the blog post here 甚至在@Powkachu 的另一个答案中也建议使用此解决方案,但在该答案中,错误是base64 协议标识符被加倍(the FileReader::readAsDataURL already returns this in the result)并不必要地传递给bypassSecurityTrustUrl来自消毒剂。

正确、安全且更简单的解决方案如下所示:

let mySrc;
const reader = new FileReader();
reader.readAsDataURL(blob); 
reader.onloadend = function() {
   // result includes identifier 'data:image/png;base64,' plus the base64 data
   mySrc = reader.result;     
}

其中 blob 是 BlobmySrc 是现在可以立即设置为图像 src 的 dataUrl(base64 字符串):

<img [src]="mySrc" />

Here a fiddle in plain Javascript 演示此解决方案在没有 Angular 的情况下的工作原理。

【讨论】:

  • 感谢您的帮助。
  • 它解决了我的问题,我认为这应该标记为正确答案
【解决方案3】:

这条线对我来说是正确的,我没有完全测试过,也不知道安全问题或任何其他东西,但它似乎是正确的:

this.ImageSource = window.URL.createObjectURL(blob);

在html中

<img [src]="ImageSource" />

更新 1:

它有安全问题,浏览器异常关于不安全的url

更新 2

问题解决了, 我的问题是,Blob 的重复信息,所以只是删除了不需要的文本,(我认为因为我正确设置了 mime,该信息已经存在)]

var result = (<any>e.srcElement).result;
  //this.Content = this.sanitizer.bypassSecurityTrustUrl('data:image/' + this.File.FileType + ';base64,' + result);
  this.Content = this.sanitizer.bypassSecurityTrustUrl(result);

【讨论】:

    【解决方案4】:

    对我有用的是这个

    let blob = new Blob([response] );
                    this.srcImage = this._sanitizer.bypassSecurityTrustUrl(window.URL.createObjectURL(blob));
    

    我使用相同的原理从 blob 下载文件,它工作正常。

    downloadFileView(data, fileName) {
            let blob:any = new Blob([data] );
            const url= window.URL.createObjectURL(blob);
            let a = document.createElement('a');
            a.href = url;
            a.download = fileName;
            a.click();
            window.URL.revokeObjectURL(url);
            a.remove();
        }
    

    如果您想查看图像并避免浏览器的警告,sanitazier 服务很重要。

    希望这对你有用

    【讨论】:

      猜你喜欢
      • 2013-05-11
      • 2013-03-28
      • 1970-01-01
      • 2019-11-18
      • 2017-11-13
      • 2020-06-16
      • 2021-12-05
      • 2021-12-06
      • 2011-05-01
      相关资源
      最近更新 更多