How can I upload images to my array, I was using axios and JSONPlaceholder but what if I have my own array in a file named data.js and I want to upload the images in there, what would I write instead of axios.post() ?
import React, { Component } from 'react';
import axios from 'axios';
import './App.css';
class App extends Component {
state = {
selectedFile: null
}
fileSelectedHandler = event => {
this.setState({ selectedFile: event.target.files[0] });
}
fileUploadHandler = () => {
const fd = new FormData();
fd.append("image", this.state.selectedFile, this.state.selectedFile.name);
axios.post("https://jsonplaceholder.typicode.com/photos", fd, {
onUploadProgress: progressEvent => {
console.log("upload progress " + Math.round((progressEvent.loaded / progressEvent.total)*100) + "%");
}
})
.then(res => {
console.log(res);
});
}
render() {
return (
<div className="App">
<input type = "file" onChange = {this.fileSelectedHandler} />
<button onClick = {this.fileUploadHandler}>upload</button>
</div>
);
}
}
export default App;