My goal is to send the uploaded image to the backend, but after appending it to formData, the formData looks empty. But logging the file shows there is an image in the state.
Code:
function UpdateProfile() {
const [file, setFile]: any = useState(null);
const handleSubmit = () => {
const formData = new FormData();
console.log(file);
formData.append("profile", "file");
console.log("formData", formData);
};
return (
<>
<Modal>
<ModalBody pb={6}>
<FormControl>
<FormLabel>Profile Picture</FormLabel>
<Input
name="profile"
type="file"
onChange={(e: any) => setFile(e.target.files[0])}
/>
</FormControl>
</ModalBody>
</Modal>
</>
);
}
The above console.log(file)
logs,
File {name: 'burger 2.png', lastModified: 1658142356471, lastModifiedDate: Mon Jul 18 2022 16:35:56 GMT+0530 (India Standard Time), webkitRelativePath: '', size: 156243, …}
But logging formData,
formData {}
its empty.
CodeSandbox : https://codesandbox.io/s/musing-chatelet-bw8lh4?file=/src/App.js