Created
August 17, 2021 13:21
-
-
Save luan0ap/5969e59c1f32599d3d5e04e8350ce962 to your computer and use it in GitHub Desktop.
Asyncronous convert an array buffer to a base64 string
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {ArrayBuffer | Array} arrayBufferToBase64 | |
* @return Promise<string> | |
*/ | |
const arrayBufferToBase64 = (buffer = new ArrayBuffer()) => { | |
return new Promise((resolve, reject) => { | |
try { | |
let binary = '' | |
const bytes = new Uint8Array(buffer) | |
for (let i = 0; i < bytes.byteLength; i++) { | |
binary += String.fromCharCode(bytes[i]) | |
} | |
return resolve(window.btoa(binary)) | |
} catch (e) { | |
reject(e) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment