Skip to content

Instantly share code, notes, and snippets.

@Lvcios
Created April 13, 2021 23:30
Show Gist options
  • Select an option

  • Save Lvcios/97cf3309acc755b87c3d744f99066c2a to your computer and use it in GitHub Desktop.

Select an option

Save Lvcios/97cf3309acc755b87c3d744f99066c2a to your computer and use it in GitHub Desktop.
import React from 'react';
class Home extends React.Component {
constructor(props) {
super(props)
this.state = {
file: null
}
this.sendFile = this.sendFile.bind(this)
}
sendFile(event) {
event.preventDefault();
let formData = new FormData();
formData.append('myFile', this.state.file);
fetch('/api/uploadFile/excel', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
console.log(data)
})
.catch(error => {
console.error(error)
})
}
render() {
return (
<div>
<form onSubmit={this.sendFile}>
<input type="file" name="file" onChange={e => this.setState({ file: e.target.files[0] })} />
<button type="submit">Enviar</button>
</form>
</div>
);
}
}
export default Home;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment