Skip to content

Instantly share code, notes, and snippets.

@RayyanNafees
Created August 22, 2021 18:26
Show Gist options
  • Save RayyanNafees/8c7993d2a361b0a5407927c737cee7df to your computer and use it in GitHub Desktop.
Save RayyanNafees/8c7993d2a361b0a5407927c737cee7df to your computer and use it in GitHub Desktop.
const b64 = file => new Promise((resolve, reject)=>{
const reader = new FileReader();
reader.onload = ()=>resolve(reader.result);
reader.onerror = () => reject(reader.result);
reader.readAsDataURL(file)
});
@RayyanNafees
Copy link
Author

A promise for creating getting base64 of a file like

const file = document.querySelector('input[type="file"]').files[0];
b64(file)
   .then(data => console.log(data))  // logs the base64 string of the supplied object into the console

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment