blob到base64转换javascript

时间:2017-06-19 11:33:20

标签: javascript html5 base64 blob

我试图在javascript中将blob对象解析为base64字符串。请帮忙。我的代码是

var reader = new FileReader();

reader.addEventListener("loadend", function () {
    // reader.result contains the contents of blob as a typed array
    var buffer = reader.result;
    var view = new Uint8Array(buffer);
    var binary = String.fromCharCode.apply(window, view);
    var base64 = btoa(binary);
    cb(base64);
    console.log(base64);
}); 

reader.readAsArrayBuffer(data.blob);

1 个答案:

答案 0 :(得分:2)

你可以试试这个 -

var blob = //your blob data;

var reader = new FileReader();

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

参考 - Convert blob to base64