Last active
March 18, 2019 07:01
-
-
Save hossammourad/e4c403c6dda5053a0cd6f052f675475b to your computer and use it in GitHub Desktop.
[Client-Side]: from file URL to Base64
This file contains 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
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