sandbox-react/form-csv-form-data/app/components/form.jsx

43 lines
1019 B
JavaScript

import React, {useState} from 'react';
import axios from 'axios';
const Form = () => {
const [csvFile, setCsvFile] = useState(null);
const formData = new FormData();
if(csvFile){
console.log('Form: csvFile available');
}else{
console.log('Form: csvFile NOT available');
}
const handleChange = (e) => {
if(e.currentTarget.files){
console.log('Form: csvFile available');
setCsvFile((csvFile) => e.currentTarget.files[0]);
}else{
console.log('Form: csvFile NOT available');
}
};
const handleSubmit = (e) => {
e.preventDefault();
async function postCsv() {
const rsp = await axios.post(
'http://localhost:65535/form-csv-form-data',
formData,
);
console.log('Form: handleSubmit() postCsv() rsp: ' + rsp);
}
postCsv();
};
return (
<>
<h3>Form</h3>
<form onSubmit={handleSubmit}>
<input type='file' accept='.csv' onChange={handleChange} />
<button type='submit'>post</button>
</form>
</>
);
};
export default Form;