• Stars
    star
    1,001
  • Rank 45,845 (Top 1.0 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

πŸ“· ReactJS Dropzone for File Uploads (using Dropzone.js)

Dropzone.js Component for React

Build Status Dependency Status npm version Downloads

⚠️ Maintainers wanted! I haven't been able to keep this component up to date and would gladly hand the keys over to someone who is.

A Dropzone component for ReactJS, allowing users to "drag and drop" files into an upload area. The component uses the battle-tested Dropzone.js to provide a cross-browser-compatible upload component.

You can see a demo of the uploader with minimal configuration here.

Screen GIF

Usage

The component is initialized with a configuration object. Optional are a list of event handlers and a configuration object for dropzone.js.

If you are using one of the many module solutions, you can simply install and require this component like shown below. The package's main entry point is lib/dropzone.js, which gives you all the dropzone components. If you're rolling with ES6/ES2015, feel free to use src/dropzone.js. If you don't want any trouble at all, just add dist/dropzone.min.js as a script to your app and use <DropzoneComponent />.

⚠️ Ensure that React and ReactDOM are global variables, so that they can be reached on window.React or global.React. Many fancy boilerplates are overly fancy and somehow remove those variables.

If you are using a fancy boilerplate, you might want to require the lib directly, by using import DropzoneComponent from 'react-dropzone-component/lib/react-dropzone' or require('react-dropzone-component/lib/react-dropzone').

Please ensure that you also include two required CSS files: node_modules/react-dropzone-component/styles/filepicker.css and node_modules/dropzone/dist/min/dropzone.min.css. There are currently a bunch of good ways to combine and process CSS in React, so I'll leave it to you to choose whatever method is best for you - the component does not automatically load CSS.

To use this component without React-DOM, use version ^0.6 - from 0.7 on, we need it.

npm install react-dropzone-component
import React from 'react';
import ReactDOM from 'react-dom';
import DropzoneComponent from 'react-dropzone-component';

var componentConfig = {
    iconFiletypes: ['.jpg', '.png', '.gif'],
    showFiletypeIcon: true,
    postUrl: '/uploadHandler'
};

ReactDOM.render(
    <DropzoneComponent config={componentConfig}
                       eventHandlers={eventHandlers}
                       djsConfig={djsConfig} />,
    document.getElementById('content')
);

The configuration allows you to disable the display of CSS file type icons and to set the URL to which uploads should be posted.

Accessing the Dropzone Object

There are a bunch of operations that might require accessing the dropzone object, especially when wanting to call dropzone methods.

To get said object, use the init event, whose callback will receive a reference to the dropzone object as a parameter.

var myDropzone;

function initCallback (dropzone) {
    myDropzone = dropzone;
}

function removeFile () {
    if (myDropzone) {
        myDropzone.removeFile();
    }
}
Usage Without Automatic Posting

If you want to use this component without posting automatically to a URL but instead do the posting yourself, then you can just fill the postUrl option with a meaningless string and handle the displaying of progress by yourself using the provided event handlers. To see this in action, check out the examples!

var componentConfig = { postUrl: 'no-url' };
var djsConfig = { autoProcessQueue: false }
var eventHandlers = { addedfile: (file) => console.log(file) }

ReactDOM.render(
    <DropzoneComponent config={componentConfig}
                       eventHandlers={eventHandlers}
                       djsConfig={djsConfig} />,
    document.getElementById('content')
);
Custom Preview Template

The djsconfig property is compatible with all of the options in the official DropzoneJS documentation. Updating the preview template can be done as follows:

var ReactDOMServer = require('react-dom/server');

var djsConfig = {
  previewTemplate: ReactDOMServer.renderToStaticMarkup(
    <div className="dz-preview dz-file-preview">
      <div className="dz-details">
        <div className="dz-filename"><span data-dz-name="true"></span></div>
        <img data-dz-thumbnail="true" />
      </div>
      <div className="dz-progress"><span className="dz-upload" data-dz-uploadprogress="true"></span></div>
      <div className="dz-success-mark"><span>βœ”</span></div>
      <div className="dz-error-mark"><span>✘</span></div>
      <div className="dz-error-message"><span data-dz-errormessage="true"></span></div>
    </div>
  )
}
Custom Post Parameters

To add custom parameters to your request, add a params property to your Dropzone.js configuration object.

var djsConfig = {
    addRemoveLinks: true,
    params: {
        myParameter: "I'm a parameter!"
    }
};

var componentConfig = {
    postUrl: '/uploadHandler'
};

ReactDOM.render(<DropzoneComponent config={componentConfig} djsConfig={djsConfig} />, document.getElementById('content'));

Custom Dropzone Area

In case you need to customize the dropzone area, you may pass a jQuery compatible selector in the config object.

var componentConfig = {
    postUrl: '/uploadHandler',
    dropzoneSelector: 'body',
};

ReactDOM.render(
  <DropzoneComponent config={componentConfig} />,
  document.getElementById('content'),
);

The code above will use the entire page body as the dropzone area.

Callbacks

Callbacks can be provided in an object literal.

var eventHandlers = {
    // This one receives the dropzone object as the first parameter
    // and can be used to additional work with the dropzone.js
    // object
    init: null,
    // All of these receive the event as first parameter:
    drop: callbackArray,
    dragstart: null,
    dragend: null,
    dragenter: null,
    dragover: null,
    dragleave: null,
    // All of these receive the file as first parameter:
    addedfile: simpleCallBack,
    removedfile: null,
    thumbnail: null,
    error: null,
    processing: null,
    uploadprogress: null,
    sending: null,
    success: null,
    complete: null,
    canceled: null,
    maxfilesreached: null,
    maxfilesexceeded: null,
    // All of these receive a list of files as first parameter
    // and are only called if the uploadMultiple option
    // in djsConfig is true:
    processingmultiple: null,
    sendingmultiple: null,
    successmultiple: null,
    completemultiple: null,
    canceledmultiple: null,
    // Special Events
    totaluploadprogress: null,
    reset: null,
    queuecomplete: null
}

To provide a single callback, simply override one of these events with your function reference. If you want to provide multiple callbacks, simply provide an array with your function references.

var callbackArray = [
    function () {
        console.log('Look Ma, I\'m a callback in an array!');
    },
    function () {
        console.log('Wooooow!');
    }
];

var simpleCallBack = function () {
    console.log('I\'m a simple callback');
};

Updating the Component's Properties

This React Component is a wrapper around Dropzone.js - meaning that Dropzone.js is not aware of the React component life cycle. When you update the component's properties, we will use a copy of jQuery's extend method (see documentation) to merge new options into the Dropzone's properties object.

If you want to fundamentally change things about your dropzone, we recommend that you either modify the Dropzone object directly or destroy and recreate the component.

Server Example

This component comes with a small server example. To try it out, simply run npm install and npm start from the example folder. Visit http://localhost:8000/example/ to see the uploads working.

To check out the (super small) source code for this simple upload-accepting server, check out example/src-server/ and example/server.js. The component works with any server infrastructure, though!

License

MIT. For details, please consult README.md.

More Repositories

1

windows95

πŸ’©πŸš€ Windows 95 in Electron. Runs on macOS, Linux, and Windows.
TypeScript
22,225
star
2

macintosh.js

πŸ–₯ A virtual Apple Macintosh with System 8, running in Electron. I'm sorry.
JavaScript
8,280
star
3

windows-build-tools

πŸ“¦ Install C++ Build Tools for Windows using npm
TypeScript
3,398
star
4

npm-windows-upgrade

πŸš€ Upgrade npm on Windows
JavaScript
2,539
star
5

windows-development-environment

πŸ”­ Turning Windows into an environment ready for modern development
PowerShell
1,359
star
6

React-Spreadsheet-Component

πŸ“‹ Spreadsheet Component for ReactJS
JavaScript
763
star
7

electron-windows-notifications

⚑ Send native Windows WinRT notifications from Electron
JavaScript
262
star
8

slack-archive

Generate static HTML archives of Slack workspaces
TypeScript
239
star
9

project-oxford

πŸ”­ Project Oxford for Node
JavaScript
177
star
10

electron-code-editor

⚑ A tiny code editor based on Electron, in ~100 lines of code
JavaScript
159
star
11

electron-notification-state

πŸ” Should you display a notification? Let's find out!
TypeScript
149
star
12

Ghost-Azure

πŸ‘» Install Ghost on Azure with a simple click
JavaScript
129
star
13

peer-review-bot

A little bot checking GitHub pull requests for peer reviews
JavaScript
102
star
14

vsc-ember-cli

⚑ Turn Visual Studio Code into an Ember Cli IDE
TypeScript
92
star
15

razer-chroma

πŸ”₯ Control your Razer Keyboard, right from Node
C++
90
star
16

electron-uwp-background

⚑ Sample explaining how to use UWP background tasks with Electron apps
C#
82
star
17

Node-Mayhem

πŸ’£ Multiplayer HTML5 shooter with Node.js, Socket.io and Melon.js
JavaScript
69
star
18

Ghost-Updater-Azure

πŸ‘» Update Ghost on Azure Websites / Azure Web Apps with a few clicks
JavaScript
62
star
19

electron-windows-interactive-notifications

⚑ Respond to interactive notifications on Windows, from Electron
C++
57
star
20

responsive-images-generator

Generate responsive images from Node
JavaScript
55
star
21

dennard

πŸ”¬ Tiny Node cli tool to see a local app's memory footprint, cross-platform
JavaScript
54
star
22

macos-notification-state

πŸ” This native module checks if the user is active, if the screen is locked, or if "do not disturb" is enabled.
Objective-C++
53
star
23

travis-jekyll-git

Deploy Jekyll pages using Git and Travis CI
Shell
44
star
24

Reveal-PikeStreet

"Pike Street" Theme for Reveal.js, intended for Microsoft's DX's Open Source Engineers
JavaScript
44
star
25

monaco-loader

πŸ“¦ Load the Monaco editor with a module loader (require, import, etc)
JavaScript
36
star
26

introducing-electron-editor

Code used in the book "Introducing Electron"
JavaScript
36
star
27

JARVIS

Node.js Personal Assistant Prototype
JavaScript
30
star
28

windows-quiet-hours

Checks if Windows 10 quiet hours are active
JavaScript
26
star
29

parse-server-azure

Adapters, tools, and documentation to use Parse-Server with Microsoft Azure
JavaScript
23
star
30

onedrive-link

Generate direct download links for OneDrive
JavaScript
23
star
31

vsc-travis-ci-status

:shipit: Check the status of your Travis CI project from Visual Studio Code
TypeScript
23
star
32

parse-docker

🚀 Run the Open Source Parse Server in Docker
JavaScript
22
star
33

electron-windows-sample

⚑ A minimal Electron sample application showing how to make an Electron app look native on Windows
JavaScript
20
star
34

ember-cli-azure-deploy

πŸ”§ Build Ember Cli Apps on Azure Websites
Shell
19
star
35

electron-comrade

πŸ”§ Run Electron apps with different versions or builds of Electron
TypeScript
18
star
36

parse-server-azure-storage

Use Azure Blob Storage with Parse Server
JavaScript
16
star
37

windows-shortcuts-ps

Node.js: Read Windows shortcuts without native dependencies (Windows 7 and up)
JavaScript
15
star
38

parse-docker-azure

🐳 Deploy the Open Source Parse Server on Azure, using Docker
15
star
39

iojs-azure

Running io.js on Azure Websites
Shell
15
star
40

emoji-autocomplete

πŸ“‡ Node Library, turning words into emojis (turn rent into 🏠 πŸ’Έ)
JavaScript
14
star
41

windows-active-process

Check which process is currently in the foreground (focused / active)
C++
14
star
42

electron-winstore-auto-launch

πŸš€ Configure your Electron to start automatically when the user logs in
TypeScript
14
star
43

ember-cli-windows-addon

🚀 Ember Addon, Optimizing Ember Cli Performance on Windows
JavaScript
14
star
44

WinBridge

Native WinRT Controls & Features for Unity3D
C#
14
star
45

MicrosoftBand-Sample

A small sample showcasing interaction with the Microsoft Band
C#
13
star
46

sleuth

πŸ•΅οΈβ€β™‚οΈ A Slack log viewer
TypeScript
12
star
47

windows-notification-state

SHQueryUserNotificationState, but as a native Node addon
JavaScript
11
star
48

SlackbotBender

A simple chatbot for slack.
JavaScript
10
star
49

medium-stats

You want Medium stats for traffic sources, this tool will give them to you
JavaScript
10
star
50

electron-smorgasbord

πŸ“¦ A local update server for Squirrel & Electron, with a fancy GUI
TypeScript
10
star
51

lets-encrypt-iis

IIS Configurator for the wonderful Let's Encrypt project
Python
10
star
52

electron-face-detection-windows

Detect Faces in images, using Windows' very own face detection
JavaScript
9
star
53

chrome-cachetool

Chrome & Chromium's "cachetool"
TypeScript
9
star
54

Ghost-Desktop

Ghost for Desktops
JavaScript
7
star
55

azure-node4

Run Node.js 4.0 on Azure
Shell
7
star
56

modernweb

Code Demo for ModernWeb 2018 Keynote
HTML
6
star
57

javascript-course

Solutions to my little JavaScript course's exercises
JavaScript
6
star
58

electron-versions

Get Electron and Chromium versions for tags in a given repository
TypeScript
6
star
59

node-app-nap

😴 Disable macOS App Nap using Node
Objective-C++
5
star
60

electron-test-build

Take a built Electron app, replace the version of Electron
JavaScript
4
star
61

azure-npm3

Use npm3 on Azure Web Apps (formerly known as Azure Websites)
Shell
4
star
62

docker-jekyll

🐳 Jekyll with Extras for Docker
4
star
63

electron-forge-maker-nsis

An electron-forge maker for NSIS that supports electron-forge v6 and can be used as a replacement for electron-builder. Supports code-signing and basic updates with electron-updater.
TypeScript
4
star
64

electron-squirrel-s3

Update your Electron app directly from S3
TypeScript
4
star
65

UnityGettingStarted

Code for the TR Unity Presentation
C#
4
star
66

nodesummit-workshop

JavaScript
4
star
67

waffle

JavaScript
4
star
68

windows-process-info

In Node.js, quickly check if a process is running on win32. Fast, small, native.
JavaScript
4
star
69

shout-azure

πŸ”ˆ Deploy Shout to Azure
3
star
70

AndroidAzureTweets

A sample Twitter-Style app using Azure Mobile Services for Android
Java
3
star
71

mountaingiantweb

JavaScript
3
star
72

find-npm-windows

πŸ“¦ Find npm on Windows
JavaScript
3
star
73

dengue-research-portal

πŸ’» A researcher's portal into dengue fever history and machine learning prediction
JavaScript
3
star
74

internet-explorer-version

Find the installed version of Internet Explorer, using Node.js
JavaScript
3
star
75

wrapped

A silly "wrapped" generator for my team
TypeScript
3
star
76

chrome-software-rendering-list

πŸ™… Chromium's list of blacklisted GPUs, consumable as a Node module
JavaScript
2
star
77

Unity-WinRT-WP8-PlayMovieNatively

Playing a movie on WinRT from within Unity
C#
2
star
78

atom-ghost

Write your blog posts in Atom, publish with Ghost
JavaScript
2
star
79

slack-wechat-bridge

TypeScript
2
star
80

SmoothRawMouse-UnityWindows8

Smoothened Mouse device delta readings in Unity for Windows 8 Store
C#
2
star
81

DJWindows98

A Mini Stage Show for DJ Windows 98
C#
2
star
82

node12-azure

Run Node 0.12 (Stable) on Azure Websites
Shell
2
star
83

adfreeclient

Client for AdFree, TechCrunch Disrupt Hackathon Project
CSS
2
star
84

LockLogger

A little debugging tool to log your session status
JavaScript
2
star
85

ember-visual-studio

A Visual Studio extension for Ember and Ember Cli
C#
1
star
86

ghost-arm

Ghost for ARM
JavaScript
1
star
87

sailsjs-azure

SailsJS, deployable to Azure
JavaScript
1
star
88

test-windows-linux-subsystem

⚑ We should probably test this Linux thing we just got
Shell
1
star
89

buy-my-vanagon

https://www.1979vanagon.com
HTML
1
star
90

SquirrelCLSIDForAppUserModelId

Quickly see what ToastActivatorCLSID Squirrel would generate for your AppUserModelId
C#
1
star
91

electron-drone

JavaScript
1
star
92

sanitize-xml-string

Sanitize a string for use with XML
TypeScript
1
star
93

npm-config-arguments

Parses npm_config env variables and assigns them to argv - useful for 'npm run' commands
JavaScript
1
star
94

kona

πŸƒ Kona A Slack Bot for Strava
TypeScript
1
star
95

tile

C++
1
star
96

electron-slack-tutorial

TypeScript
1
star
97

OxideFleet

Oxide, but for Car Management
JavaScript
1
star
98

runtime-logo

A logo for Slack's Desktop Runtime team
JavaScript
1
star
99

electron-react-playground

TypeScript
1
star
100

ember-service-worker-ghost-cache

JavaScript
1
star