Last active
November 18, 2021 16:41
-
-
Save kingisaac95/bb248fa1fe8393a0fbc1b4556a68ecf7 to your computer and use it in GitHub Desktop.
File Reader to Base64
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* @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