Last active
December 30, 2015 03:27
-
-
Save jovey-zheng/a0fd0903c41f92a97a14 to your computer and use it in GitHub Desktop.
FileReader() sample
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
handleFileChoose() { | |
const fileReader = new FileReader(); | |
const imgUrl = document.getElementById('openFile').files[0]; | |
if (imgUrl) { | |
fileReader.readAsDataURL(imgUrl); | |
fileReader.onloadend = () => { | |
document.getElementById('avatarImg').src = fileReader.result; | |
} | |
} | |
} |
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
<div> | |
<img id="avatarImg" /> | |
<label htmlFor="openFile">EDIT</label> | |
<input id="openFile" type="file" onChange={() => this.handleFileChoose()} /> | |
</div> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment