base64和Blob转换 #73
Anuluca Date : 2020-10-22 Tags : 2
base64转Blob
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function base64ToBlob(base64, mimeType) { let byteString = atob(base64.split(',')[1]); let ab = new ArrayBuffer(byteString.length); let ia = new Uint8Array(ab); for (let i = 0; i < byteString.length; i++) { ia[i] = byteString.charCodeAt(i); } return new Blob([ab], { type: mimeType }); }
const base64Data = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUA..."; const blob = base64ToBlob(base64Data, 'image/png');
|
Blob转base64
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| function blobToBase64(blob) { return new Promise((resolve, reject) => { const reader = new FileReader(); reader.onloadend = function () { resolve(reader.result); }; reader.onerror = function (error) { reject(error); }; reader.readAsDataURL(blob); }); }
const blob = new Blob(['Hello, world!'], { type: 'text/plain' });
blobToBase64(blob).then(base64String => { console.log(base64String); }).catch(error => { console.error('转换失败:', error); });
|