Home > Net >  React: How to access file that gets passed in to html input type=file
React: How to access file that gets passed in to html input type=file

Time:06-21

If I have something like <input type='file' onChange={function}> How can I access the file that eventually gets passed in?

So I could do something like this:

const function = () =>{
listOfFiles.add(file)
}

I would like to retrieve whatever files are passed to the input/file uploader and add them to a list. But I am not sure how to access the actual file that a user uploads.

CodePudding user response:

EDITED: (removed setIsSelected(true);, its not needed) I think you can use react states, it will make your life easier. Try this one:

const [selectedFile, setSelectedFile] = useState();
const changeHandler = (event) => {
        setSelectedFile(event.target.files[0]);

    };
<input type="file" name="file" onChange={changeHandler} />

Your file will be in the selectedFile variable.

  • Related