Skip to content

Instantly share code, notes, and snippets.

@buglessir
Created December 18, 2019 11:25
Show Gist options
  • Save buglessir/0f34141b15bee7cc69b87944c3c1f22d to your computer and use it in GitHub Desktop.
Save buglessir/0f34141b15bee7cc69b87944c3c1f22d to your computer and use it in GitHub Desktop.
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