Skip to content

Instantly share code, notes, and snippets.

@edvaldoszy
Last active February 2, 2018 00:27
Show Gist options
  • Select an option

  • Save edvaldoszy/06787f39db1690da9830769ee2341ee4 to your computer and use it in GitHub Desktop.

Select an option

Save edvaldoszy/06787f39db1690da9830769ee2341ee4 to your computer and use it in GitHub Desktop.
import React, { Component } from 'react';
import logo from './logo.svg';
import './App.css';
import axios from 'axios';
class App extends Component {
onInputChange = ({ target }) => {
const { files } = target;
for (let i = 0; i < files.length; i++) {
this.sendFileRequest(files[i], i);
}
}
sendFileRequest = (file, id) => {
const data = new FormData();
data.append('imagem', file);
const config = {
onUploadProgress: function(progressEvent) {
var percentCompleted = Math.round( (progressEvent.loaded * 100) / progressEvent.total );
console.log(`${id}: ${percentCompleted}`);
}
};
axios.post('http://localhost:3001/api/imoveis/uploads', data, config)
.then(res => {
console.log(res.data);
})
.catch(err => {
console.error(err);
});
}
render() {
return (
<div className="App">
<header className="App-header">
<img src={logo} className="App-logo" alt="logo" />
<h1 className="App-title">Welcome to React</h1>
</header>
<form method="post" encType="multipart/form-data">
<input type="file"
multiple
onChange={this.onInputChange} />
</form>
</div>
);
}
}
export default App;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment