Skip to content

Instantly share code, notes, and snippets.

@Deliaz
Created December 16, 2016 06:12
Show Gist options
  • Save Deliaz/e89e9a014fea1ec47657d1aac3baa83c to your computer and use it in GitHub Desktop.
Save Deliaz/e89e9a014fea1ec47657d1aac3baa83c to your computer and use it in GitHub Desktop.
Convert array buffer to base64 string
function arrayBufferToBase64(buffer) {
let binary = '';
let bytes = new Uint8Array(buffer);
let len = bytes.byteLength;
for (let i = 0; i < len; i++) {
binary += String.fromCharCode(bytes[i]);
}
return window.btoa(binary);
}
@arturzhitkovcih
Copy link

     let base64String = btoa(String.fromCharCode(...file));

@Lekha30
Copy link

Lekha30 commented Sep 29, 2021

btoa() is deprecated

@caebwallace
Copy link

btoa() is deprecated

Can you give your source please ?
https://developer.mozilla.org/en-US/docs/Web/API/btoa

@movsb
Copy link

movsb commented May 14, 2024

@caebwallace btoa can only process single byte character. Any other Unicode characters will cause it to throw exception: btoa('🙁').

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment