Created
June 4, 2015 07:46
-
-
Save svnlto/5c62c0ecfcb741a5eef9 to your computer and use it in GitHub Desktop.
FileReader Promise Helper
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 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