Skip to content

Instantly share code, notes, and snippets.

@kingisaac95
Last active November 18, 2021 16:41
Show Gist options
  • Save kingisaac95/bb248fa1fe8393a0fbc1b4556a68ecf7 to your computer and use it in GitHub Desktop.
Save kingisaac95/bb248fa1fe8393a0fbc1b4556a68ecf7 to your computer and use it in GitHub Desktop.
File Reader to Base64
/*
* @params - event: upload file event
* @params - onComplete: callback provided by caller
*/
const uploadFile = ({ event, onComplete }) => {
event.preventDefault();
var base64File = null;
const file = event.target.files[0];
if (file) {
const reader = new FileReader();
reader.readAsBinaryString(file);
reader.onload = (readerEvent) => {
base64File = btoa(readerEvent.target.result);
onComplete({ file, base64File });
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment