1

I want the user to "upload" a file from their computer, store it in the browser (I guess) and display the image, without sending it to a server. Basically, I want this to happen (example on the website): https://www.javascripture.com/FileReader

This works by itself, but I am using react-dropzone and it's not working.

My code:

import React, { useCallback, useState } from "react";
import { useDropzone } from "react-dropzone";
import styles from "./dropzone.module.css";
import ImageZone from "../imagezone";

export default function Dropzone() {
  const [path, setPath] = useState("");
  const openFile = () => {
    const reader = new FileReader();
    reader.onload = () => {
      const dataURL = reader.result;
      setPath(dataURL);
    };
  };
  const onDrop = useCallback(acceptedFiles => {
    openFile();
  }, []);
  const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });

  return (
    <div className={styles.mycontainer}>
      <div className={styles.dropzone} {...getRootProps()}>
        <input {...getInputProps()} />
        {isDragActive ? (
          <p>Drop the files here ...</p>
        ) : (
          <p>Drag 'n' drop some files here, or click to select files</p>
        )}
      </div>
      <ImageZone src={path}></ImageZone>
    </div>
  );
}

6
  • 1
    What do you mean with store it in the browser ? Commented Oct 24, 2019 at 14:44
  • I don't really know, that's probably not what I even want. I just want the user to see the picture they uploaded without it being sent to a server. Just in the browser. Commented Oct 24, 2019 at 14:45
  • So just preview the image dropped into the DZ Commented Oct 24, 2019 at 14:46
  • In your openFile function you're creating a file reader and setting what it does when it's finished reading... but where are you actually giving it the file to read and calling the function to begin reading? Commented Oct 24, 2019 at 14:47
  • what does your setPath function look like? Commented Oct 24, 2019 at 14:49

3 Answers 3

8

Use URL.createObjectURL(file) on the files returned from the onDrop callback.

import React, { useCallback, useState } from "react";
import { useDropzone } from "react-dropzone";

export default function Dropzone() {
  const [paths, setPaths] = useState([]);

  const onDrop = useCallback(acceptedFiles => {
    setPaths(acceptedFiles.map(file => URL.createObjectURL(file));
  }, [setPaths]);
  const { getRootProps, getInputProps, isDragActive } = useDropzone({ onDrop });

  return (
    <div>
      <div {...getRootProps()}>
        <input {...getInputProps()} />
        <p>Drop the files here ...</p>
      </div>
      {paths.map(path => 
        <img key={path} src={path} />
       }
    </div>
  );
}

See https://react-dropzone.js.org/#!/Previews for more info.

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

1 Comment

i was using multiple: false for single image so had to just use acceptedFiles[0] to make it work :)
1

In 2022: See this great tutorial https://blog.logrocket.com/create-drag-and-drop-component-react-dropzone/#create-drag-and-drop-and-image-preview-functionality

Comments

0

You can use file reader api to read the content of droped file and then show it. You need to fix onload callback

  const onDrop = useCallback(acceptedFiles => {

   const reader = new FileReader();
   reader.onload = e => {
      const dataURL = e.target.result;
      setPath(dataURL);
   };
    acceptedFiles.forEach(file => reader.readAsArrayBuffer(file))
  }, []);

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.