Created
December 18, 2019 11:25
-
-
Save buglessir/0f34141b15bee7cc69b87944c3c1f22d to your computer and use it in GitHub Desktop.
This file contains hidden or 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'; | |
import { render } from 'react-dom'; | |
class App extends React.Component { | |
onChangeFile = (e) => { | |
this.setState({ | |
selectedFile: e.target.files[0] | |
}); | |
} | |
upload = () => { | |
const formData = new FormData(); | |
formData.append('image', this.state.selectedFile); | |
fetch('http://localhost/react_upload/upload.php', { | |
method: 'POST', | |
body: formData | |
}) | |
.then(response => { | |
console.log(response); | |
}); | |
} | |
render() { | |
return ( | |
<> | |
<h1>Upload Image:</h1> | |
<input type="file" onChange={this.onChangeFile} /> | |
<button onClick={this.upload}>upload</button> | |
</> | |
); | |
} | |
} | |
render( <App />, document.getElementById('root') ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment