Created
December 13, 2018 15:23
-
-
Save merecarvill/c5d81ecadc589aede9908b3c8a868b0d to your computer and use it in GitHub Desktop.
Bill upload view example
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
import React from "react"; | |
export default class BillUploadView extends React.Component { | |
constructor(props) { | |
super(props); | |
this.handleSubmit = this.handleSubmit.bind(this); | |
this.handleImageChange = this.handleImageChange.bind(this); | |
this.state = { | |
file: "", | |
imagePreviewUrl: "" | |
}; | |
} | |
render() { | |
const { imagePreviewUrl } = this.state; | |
return ( | |
<div> | |
<input type="file" onChange={this.handleImageChange} /> | |
<button type="button" onClick={this.handleSubmit}> | |
Upload Image | |
</button> | |
{ | |
imagePreviewUrl ? | |
<img src={imagePreviewUrl} /> : | |
<div>Please select an Image for Preview</div> | |
} | |
</div> | |
); | |
} | |
handleSubmit(e) { | |
e.preventDefault(); | |
console.log("handle uploading-", this.state.file); | |
} | |
handleImageChange(e) { | |
e.preventDefault(); | |
const reader = new FileReader(); | |
const file = e.target.files[0]; | |
reader.onloadend = () => { | |
this.setState({ | |
file, | |
imagePreviewUrl: reader.result | |
}); | |
}; | |
reader.readAsDataURL(file); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment