I'm trying to use FormData to upload an image and save it to a specific folder on my localhost. I can see that the data is there as Console Log shows me the name and other info. I think the problem is with axios.post since console is telling me the upload location on my localhost is not found. Just not sure what to do about it.
Here is my App.js file:
import axios from 'axios';
import React,{Component} from 'react';
class App extends Component {
state = {
// Initially, no file is selected
selectedFile: null
};
// On file select (from the pop up)
onFileChange = event => {
// Update the state
this.setState({ selectedFile: event.target.files[0] });
};
// On file upload (click the upload button)
onFileUpload = () => {
// Create an object of formData
const formData = new FormData();
// Update the formData object
formData.append(
"myFile",
this.state.selectedFile,
this.state.selectedFile.name
);
// Details of the uploaded file
console.log(this.state.selectedFile);
// Request made to the backend api
// Send formData object
axios.post('/home3/patriult/public_html/', formData);
};
render() {
return (
<div>
<form method="post" encType="multipart/form-data">
<input type="file"/>
<button onClick={this.onFileUpload}>
Upload!
</button>
</form>
</div>
);
}
}
export default App;