1

So in HTML I can do something this:

<input type="file" id="upload" accept="text" multiple/>

Then I can access the files uploaded in JavaScript like this whenever the input changes:

document.getElementById('upload').onchange = function(){
  const uFiles = this.files;
  console.log(uFiles);
}

How would I go about doing that with ReactJS? So far, I've tried using the same approach I would use for HTML and JavaScript but I get the response that uFiles is undefined rather than a FileList object.


In my React Render:

<input onChange={this.doFunc} id="upload" type="file" accept="text" multiple />

In my React class:

doFunc = () => {

  const uFiles = this.files;
  console.log(uFiles);

}

2 Answers 2

1

I think you have to use event.

   doFunc = e => {

      const uFiles = e.target.files;
      console.log(uFiles);

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

Comments

0
function handleUploadChange(e)
{
    const file = e.target.files[0];
    if ( !file )
    {
        return;
    }
    const reader = new FileReader();
    reader.readAsBinaryString(file);

    reader.onload = () => {
        console.log(reader.result);
        console.log(file.type);
    };

    reader.onerror = function () {
        console.log("error on load image");
    };
}

1 Comment

May I request you to please add some more context around your answer. Code-only answers are difficult to understand. It will help the asker and future readers both if you can add more information in your post. See also Explaining entirely code-based answers.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.