2

I'm using the following code to retrieve data from a dropped/selected file.

onDrop = (files) => {
  files.forEach(file => {
    const reader = new FileReader();
    reader.onload = () => {
      const fileAsBinaryString = reader.result
      console.log(fileAsBinaryString);
    }
    reader.onabort = () => console.log('file reading was aborted');
    reader.onerror = () => console.log('file reading has failed');

    try {
      reader.readAsDataURL(file);
    } catch(err) {
      console.log(err)
      console.log(file);
    }

    this.setState({
      fileName: file.name
    })
  });
}

render() {
  return (
    <div className="app">
      <ReactDropzone onDrop={this.onDrop} className="dropzone">
        <IconContext.Provider value={{ size: "5em" }}>
          <IoMdCloudUpload/>
        </IconContext.Provider>
        <h1>{this.state.fileName}</h1>
      </ReactDropzone>
    </div>
  );
}

When I run the server and drop something inside the dropzone, even though console.log(file) gives me a non-blank File object, I get

Error: cannot read as File: {}

At

reader.readAsDataURL(file);

Any idea as to why this may happen and how I should fix it?

1
  • I am having the same issue, did you ever find a solution? Commented Jan 5, 2019 at 8:32

1 Answer 1

2
onDrop = (acceptedFiles, rejectedFiles) => {
        const reader = new FileReader()

        reader.readAsDataURL(acceptedFiles[0])

        reader.onload = () => {
            if (!!reader.result) {
                console.log('reader.result', reader.result)
            }
        }
}
Sign up to request clarification or add additional context in comments.

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.