Skip to content

Instantly share code, notes, and snippets.

@bmorrisondev
Created April 2, 2020 16:23
Show Gist options
  • Save bmorrisondev/0f76b24844f2ad368c6da0851d2ea506 to your computer and use it in GitHub Desktop.
Save bmorrisondev/0f76b24844f2ad368c6da0851d2ea506 to your computer and use it in GitHub Desktop.
You can use this function to get a base64 encoded version of a File object for uploading to an API. Works with await, or in a promise chain.
function getDataUrl(file) {
return new Promise((resolve, reject) => {
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = () => resolve(reader.result);
reader.onerror = err => reject(err);
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment