Skip to content

Instantly share code, notes, and snippets.

@Miouyouyou
Created September 5, 2020 07:20
Show Gist options
  • Select an option

  • Save Miouyouyou/32faf510f9cf13b1fd542bf8b7a8596b to your computer and use it in GitHub Desktop.

Select an option

Save Miouyouyou/32faf510f9cf13b1fd542bf8b7a8596b to your computer and use it in GitHub Desktop.
Javascript blob to base64
/** Get a base64 string representing the content of the provided Blob
*
* @param blob
* The blob object
*
* @param cb
* A callback called with the base64 string on success
*
* @return undefined
*
* @example
* blob_to_base64(input_file_id.files[0], b64string => { console.log(b64string); });
*/
function blob_to_base64(blob, cb) {
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
const base64data = reader.result;
/* Remove the base64 header added by the Javascript API. */
const actual_data = base64data.slice(
(base64data.indexOf(";base64")+";base64".length+1));
cb(actual_data);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment