Skip to content

Instantly share code, notes, and snippets.

@luan0ap
Created August 17, 2021 13:21
Show Gist options
  • Save luan0ap/5969e59c1f32599d3d5e04e8350ce962 to your computer and use it in GitHub Desktop.
Save luan0ap/5969e59c1f32599d3d5e04e8350ce962 to your computer and use it in GitHub Desktop.
Asyncronous convert an array buffer to a base64 string
/**
* @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