I'm using react js version 17, I want to upload file and send it to my node js api. I have two buttons, The first button used to read the first line in the file, the second button is for the call to the back end server to save the file in the database as a file I have create a formData to set my formData file input always i got empty object.
Here's my code :
const [myFile,setMyFile] = useState();
const [headers,setHeaders] = useState();
const readFile = e =>{
const file = e.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
const text = e.target.result;
const headers = text.slice(0,text.indexOf('\n')).split(';');
setHeaders(headers)
}
reader.readAsText(file);
setMyFile(file)
}
const callApi = e => {
const formData = new FormData();
formData.append(
"file",
myFile,
);
axios.post('localhost:8080/uploadFile', formData, {
headers: {
'Content-Type': 'multipart/form-data'
}
})
}
My formData always return an empty object {}