JavaScript
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
.github build: Use Webpack production mode during the build (#555) Jan 19, 2018
examples docs: Add a note about async file dialog blocking in modern browsers (#… Jun 11, 2018
logo docs: Add logo to README and automate its export (#412) Apr 20, 2017
src fix(SSR): Add event listeners only if the element exist in DOM (#628) Aug 18, 2018
.babelrc feat: Add es2015 module build (#510) Oct 11, 2017
.codeclimate.yml chore: Update dependencies to enable Greenkeeper 🌴 (#294) Dec 11, 2016
.editorconfig Added .editorconfig Oct 20, 2015
.eslintrc fix(490): Put build dependencies to devDependencies (#491) Sep 6, 2017
.gitignore build: Move semantic-release to CI (#571) Feb 14, 2018
.npmignore fix: Add .babelrc to .npmignore (#558) Jan 19, 2018
.travis.yml build: Move semantic-release to CI (#571) Feb 14, 2018
LICENSE Initial commit Sep 27, 2014
README.md build: Move semantic-release to CI (#571) Feb 14, 2018
commitlint.config.js chore: Use commitlint with husky Jul 27, 2017
package.json build(styleguidist): Upgrade react-styleguidist and remove webpack (#642 Aug 18, 2018
rollup.config.js build: Bundle UMD with Rollup (#601) Aug 18, 2018
styleguide.config.js build(styleguidist): Upgrade react-styleguidist and remove webpack (#642 Aug 18, 2018
testSetup.js style: Use eslint-config-okonet and prettier (#413) May 30, 2017
wallaby.config.js test: Fix wallaby config (#505) Sep 20, 2017
yarn.lock build(styleguidist): Upgrade react-styleguidist and remove webpack (#642 Aug 18, 2018

README.md

react-dropzone logo

react-dropzone

npm Build Status codecov OpenCollective OpenCollective

Simple HTML5-compliant drag'n'drop zone for files built with React.js.

Documentation and examples: https://react-dropzone.js.org Source code: https://github.com/react-dropzone/react-dropzone/


Looking for maintainers: https://github.com/react-dropzone/react-dropzone/issues/479


Installation

Install it from npm and include it in your React build process (using Webpack, Browserify, etc).

npm install --save react-dropzone

or:

yarn add react-dropzone

Usage

Import Dropzone in your React component:

import Dropzone from 'react-dropzone'

and specify the onDrop method that accepts two arguments. The first argument represents the accepted files and the second argument the rejected files.

function onDrop(acceptedFiles, rejectedFiles) {
  // do stuff with files...
}

Files accepted or rejected based on accept prop. This must be a valid MIME type according to input element specification.

Please note that onDrop method will always be called regardless if dropped file was accepted or rejected. The onDropAccepted method will be called if all dropped files were accepted and the onDropRejected method will be called if any of the dropped files was rejected.

Using react-dropzone is similar to using a file form field, but instead of getting the files property from the field, you listen to the onDrop callback to handle the files. Simple explanation here: http://abandon.ie/notebook/simple-file-uploads-using-jquery-ajax

Specifying the onDrop method, provides you with an array of Files which you can then send to a server. For example, with SuperAgent as a http/ajax library:

onDrop: acceptedFiles => {
    const req = request.post('/upload');
    acceptedFiles.forEach(file => {
        req.attach(file.name, file);
    });
    req.end(callback);
}

Warning: On most recent browsers versions, the files given by onDrop won't have properties path or fullPath, see this SO question and this issue. If you want to access file content you have to use the FileReader API.

onDrop: acceptedFiles => {
    acceptedFiles.forEach(file => {
        const reader = new FileReader();
        reader.onload = () => {
            const fileAsBinaryString = reader.result;
            // do whatever you want with the file content
        };
        reader.onabort = () => console.log('file reading was aborted');
        reader.onerror = () => console.log('file reading has failed');

        reader.readAsBinaryString(file);
    });
}

PropTypes

See https://react-dropzone.netlify.com/#proptypes

Word of caution when working with previews

Important: react-dropzone doesn't manage dropped files. You need to destroy the object URL yourself whenever you don't need the preview by calling window.URL.revokeObjectURL(file.preview); to avoid memory leaks.

Support

Backers

Support us with a monthly donation and help us continue our activities. [Become a backer]

Sponsors

Become a sponsor and get your logo on our README on Github with a link to your site. [Become a sponsor]

License

MIT