Skip to content

Instantly share code, notes, and snippets.

@kingisaac95
Created November 18, 2021 16:48
Show Gist options
  • Save kingisaac95/aeebdf5060bb31409fc34a0ba549f5c0 to your computer and use it in GitHub Desktop.
Save kingisaac95/aeebdf5060bb31409fc34a0ba549f5c0 to your computer and use it in GitHub Desktop.
const uploadFile = ({
event,
onComplete
}: {
event: ChangeEvent<HTMLInputElement>;
onComplete: (file: File, base64File: string) => void;
}) => {
event.preventDefault();
let base64File: string;
const file: File | null = event.target.files ? event.target.files[0] : null;
if (file) {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => {
base64File = btoa(reader.result as string);
onComplete(file, base64File);
};
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment