Skip to content

Instantly share code, notes, and snippets.

@svnlto
Created June 4, 2015 07:46
Show Gist options
  • Save svnlto/5c62c0ecfcb741a5eef9 to your computer and use it in GitHub Desktop.
Save svnlto/5c62c0ecfcb741a5eef9 to your computer and use it in GitHub Desktop.
FileReader Promise Helper
const readFile = (file) => {
let reader = new global.FileReader();
return new Promise((resolve, reject) => {
reader.onload = (event) => {
file.data = event.target. result;
resolve(file);
};
reader.onerror = () => {
return reject(this);
};
if (/^image/.test(file.type)) {
reader.readAsDataURL(file);
} else {
reader.readAsText(file);
}
})
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment