I\'m new to react js. I want to upload image asynchronously with react js Suppose I have this code
var FormBox = React.createClass({
getInitialState: funct
You can make use of FileReader
var FormBox = React.createClass({
getInitialState: function () {
return {
file: '',
imagePreviewUrl: ''
}
},
pressButton: function () {
e.preventDefault();
// TODO: do something with -> this.state.file
console.log('handle uploading-', this.state.file);
},
getPhoto: function (e) {
e.preventDefault();
let reader = new FileReader();
let file = e.target.files[0];
reader.onloadend = () => {
this.setState({
file: file,
imagePreviewUrl: reader.result
});
}
reader.readAsDataURL(file);
},
render: function () {
let {imagePreviewUrl} = this.state;
let imagePreview = null;
if (imagePreviewUrl) {
imagePreview = (
);
} else {
imagePreview = (Please select an Image for Preview);
}
return (
{imagePreview}
)
}
})
ReactDOM.render( , document.getElementById('root'))