Skip to content

Instantly share code, notes, and snippets.

@hossammourad
Last active March 18, 2019 07:01
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 hossammourad/e4c403c6dda5053a0cd6f052f675475b to your computer and use it in GitHub Desktop.
Save hossammourad/e4c403c6dda5053a0cd6f052f675475b to your computer and use it in GitHub Desktop.
[Client-Side]: from file URL to Base64
const getBase64 = fileURL =>
new Promise(async resolve => {
const response = await fetch(fileURL);
const blob = await response.blob();
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = () => {
const base64data = reader.result;
resolve(base64data);
};
});
// Usage
const getFileBase64 = async () => {
const result = await getBase64('http://localhost:3000/images/sample.jpeg');
console.log(result);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment