这是一个代码片段,我想做Blob到Base64字符串:
这个注释部分可以工作,当它生成的URL被设置为img src时,它会显示图像:
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);
问题在于较低的代码,生成的源变量为空
更新:
有一个更简单的方法来做到这一点与JQuery能够创建Base64字符串从Blob文件如在上面的代码?
另一种方法是使用一个简单的包装器来包装返回Observable的FileReader(代码片段在TypeScript中):
function toBase64(blob: Blob): Observable<string> {
const reader = new FileReader();
reader.readAsDataURL(blob);
return fromEvent(reader, 'load')
.pipe(map(() => (reader.result as string).split(',')[1]));
}
用法:
toBase64(blob).subscribe(base64 => console.log(base64));
var reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
var base64data = reader.result;
console.log(base64data);
}
将文档readAsDataURL编码为base64
作为一个可等待的函数:
function blobToBase64(blob) {
return new Promise((resolve, _) => {
const reader = new FileReader();
reader.onloadend = () => resolve(reader.result);
reader.readAsDataURL(blob);
});
}
注意:如果不首先删除Base64编码数据之前的data - url声明,blob的结果不能直接解码为Base64。为了只检索Base64编码的字符串,首先从结果中删除data:/; Base64。
如果你的“blob”是一个实际的blob对象,而不是一个blob url,转换非常简单:
const reader = new FileReader()
reader.readAsDataURL(blob)
reader.onload = () => resolve(reader.result)
Blob对象示例:
console.log(blob)
输出:
Blob {lastModified: 1658039931443, lastModifiedDate: Sun Jul 17 2022 08:38:51 GMT+0200 (Central European Summer Time), name: '1.jpg', size: 35493, type: 'image/jpeg'}
lastModified: 1658039931443
lastModifiedDate: Sun Jul 17 2022 08:38:51 GMT+0200 (Central European Summer Time) {}
name: "1.jpg"
size: 35493
type: "image/jpeg"
[[Prototype]]: Blob
在我的例子中,这个blob是由Compressorjs生成的(如果你需要图像压缩)。
function bufferToBinaryString(arrayBuffer){
return String.fromCharCode(...new Uint8Array(arrayBuffer));
}
(async () => console.log(btoa(bufferToBinaryString(await new Response(blob).arrayBuffer()))))();
or
function bufferToBinaryString(arrayBuffer){
return String.fromCharCode(...new Uint8Array(arrayBuffer));
}
new Response(blob).arrayBuffer().then(arr_buf => console.log(btoa(bufferToBinaryString(arr_buf)))))
查看Response的构造函数,你可以将[blob,缓冲区源表单数据,可读流等]转换为Response,然后可以通过async方法/回调转换为[json,文本,数组缓冲区,blob]。
编辑:正如@Ralph所提到的,将所有内容转换为utf-8字符串会导致问题(不幸的是响应API没有提供转换为二进制字符串的方法),因此使用数组缓冲区作为中间,这需要更多的两个步骤(将其转换为字节数组,然后转换为二进制字符串),如果你坚持使用本机btoa方法。