Created
July 6, 2022 11:42
-
-
Save sajclarke/66004c09f1a4863b4a7c49886dd358b6 to your computer and use it in GitHub Desktop.
Convert HTML file input to binary string
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
// The below function accepts the HTMLInputElement event from a <input type="file" /> | |
// and returns a binary string | |
function readFileDataAsBase64(e) { | |
const file = e.target.files[0]; | |
return new Promise((resolve, reject) => { | |
const reader = new FileReader(); | |
reader.onload = (event) => { | |
resolve(event.target.result); | |
}; | |
reader.onerror = (err) => { | |
reject(err); | |
}; | |
reader.readAsBinaryString(file); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment