将blob转换为base64 [英] Convert blob to base64

查看:872
本文介绍了将blob转换为base64的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我想要执行的代码的片段 Blob Base64 字符串:

This is a snippet for the code that I want to do Blob to Base64 string:

这个评论的部分有效,当由此生成的URL设置为img src时,它会显示图像:

This commented part works and that when the URL generated by this is set to img src it displays the image:

var blob = items[i].getAsFile();
//var URLObj = window.URL || window.webkitURL;
//var source = URLObj.createObjectURL(blob);
//console.log("image source=" + source);

var reader = new FileReader();
reader.onload = function(event){
console.log(event.target.result)
}; // data url!
var source = reader.readAsBinaryString(blob);

问题在于代码越低,生成的源变量为null

The problem is with the the lower code, the source variable generated is null

更新:

使用JQuery是否有更简单的方法可以创建Base64字符串来自Blob文件,如上面的代码?

Is there an easier way to do this with JQuery to be able to create Base64 String from Blob file as in the code above?

推荐答案

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

形成 docs readAsDataURL 编码为base64

Form the docs readAsDataURL encodes to base64

这篇关于将blob转换为base64的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆