Skip to content

Instantly share code, notes, and snippets.

@sajclarke
Created July 6, 2022 11:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sajclarke/66004c09f1a4863b4a7c49886dd358b6 to your computer and use it in GitHub Desktop.
Save sajclarke/66004c09f1a4863b4a7c49886dd358b6 to your computer and use it in GitHub Desktop.
Convert HTML file input to binary string
// 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