8

I'm using a form control component to pass an image to an API endpoint via an AJAX call. So far, I'm unable to find information on how to actually target the actual file data.

2 Answers 2

12

You can get the file via onChange:

<FormControl type="file" onChange={(e) => console.log(e.target.files)}/>

where e.target.files is a FileList containing 0 or 1 files.

Sign up to request clarification or add additional context in comments.

Comments

1

I'm using TypeScript, and I kept getting the error Property 'files' does not exist on type 'EventTarget & FormControlElement'. Including the following types solved the issue:

const [thumbnail, setThumbnail] = useState<FileList | null>(null);
...
<Form.Control type="file" onChange={(e: React.ChangeEvent<HTMLInputElement>) => setThumbnail(e.target.files)} />

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.