Hi I need to upload file in react using class component as it is legacy application. I need this file in UI only so I dont want to save it into database. I have to save in the public folder also under public folder I need to give fucntionality to user to give file and folder name.
What changes I need to do in below code. Below code is perfectly working. Only thing is I need to upload file to folder and need to manaully enter file and folder name.
import axios from 'axios';
import React, { Component } from 'react';
class FileUpload extends Component {
state = {
selectedFile: null,
};
onFileChange = (event) => {
this.setState({ selectedFile: event.target.files[0] });
};
onFileUpload = () => {
const formData = new FormData();
formData.append(
'myFile',
this.state.selectedFile,
this.state.selectedFile.name
);
console.log(this.state.selectedFile);
axios.post('api/uploadfile', formData); //I need to change this line
};
fileData = () => {
if (this.state.selectedFile) {
return (
<div>
<h2>File Details:</h2>
<p>File Name: {this.state.selectedFile.name}</p>
<p>File Type: {this.state.selectedFile.type}</p>
<p>
Last Modified:{' '}
{this.state.selectedFile.lastModifiedDate.toDateString()}
</p>
</div>
);
} else {
return (
<div>
<br />
<h4>Choose before Pressing the Upload button</h4>
</div>
);
}
};
render() {
return (
<div>
<h1>Plese select the translation file</h1>
<div>
<input type="file" onChange={this.onFileChange} />
<button onClick={this.onFileUpload}>Upload!</button>
</div>
{this.fileData()}
</div>
);
}
}
export default FileUpload;
Edit 1:-
Edit 2:-
app.listen(80, () => console.log('Listening on port 80'));
axios.post('http://localhost:80/api/uploadfile', formData, {

