Skip to content

Instantly share code, notes, and snippets.

@jovey-zheng
Last active December 30, 2015 03:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jovey-zheng/a0fd0903c41f92a97a14 to your computer and use it in GitHub Desktop.
Save jovey-zheng/a0fd0903c41f92a97a14 to your computer and use it in GitHub Desktop.
FileReader() sample
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;
}
}
}
<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