Drag And Drop File Upload: A Comprehensive Guide

Of course, you can always upload files or images using traditional methods, but, for many users, drag and drop is a more comfortable way to select and upload a file. In fact, most users now expect to use the drag and drop feature when they upload files to a website.

Whether they are uploading a job application to a company website or adding images and videos on social media, every web user needs to upload files.  Furthermore, in every use case, they expect a user-friendly file upload feature to make it happen.

Because it is such a universally necessary feature, let’s take a look at the anatomy of the drag and drop file upload process. We will help you discover some must-have drag and drop file upload features. We will also look at some describe traditional approaches to implementing file uploads on your website. Finally, we will look at how the Filestack File Uploader may be your best option. 

What are drag and drop file uploads?

Drag and drop file uploads happen when you drag one or more files from an underlying platform’s file manager and drop them onto a web page for upload. A preview of the image indicates the upload has begun.

Many JavaScript libraries create this type of drag and drop file upload feature with a few lines of code. 

Why need the Drag and Drop file upload feature?

The drag and drop file upload feature is easier to use than a traditional file upload. It provides a better user experience and looks better too.

Websites need this feature to accept user-generated content like product photos on eBay, user avatars on Facebook, CVs or portfolios on job sites, and homework assignments on Skillshare, Coursera, as well as on videos YouTube. The list is endless.

What are Javascript Drag and Drop Events?

Here are some important JavaScript drag and drop events:

ondrag: It is used in HTML when an element or text selection is dragged.

ondragstart: This function is used to call the drag(event) function, which indicate the data that is to be dragged.

ondragenter: It determines whether or not the drop target will accept the drop. This event must be canceled if the drop is to be accepted.

ondragleave: This happens when the mouse leaves an element before a valid drop target while dragging.

ondragover: This specifies the location where the dragged data can be dropped.

ondrop: This specifies the location of the drop at the end of the drag operation.

ondragend: This happens after the user has finished dragging an element.

By Using Filestack how can you Drag and Drop Files in JavaScript?

By using Filestack Drag and Drop you can easily perform Drag and Drop file upload to your website and it needs only three lines of code to enable any element on your page. Filestack is a user interface for our JavaScript SDK library and for additional support check Javascript file upload examples in the SDK.

To begin with, first, you need to include our UMD module in your code. It will integrate Filestack-Drag-and-Drop with your web application:

<script src="//static.filestackapi.com/filestack-drag-and-drop-js/{MAJOR_VERSION}.x.x/filestack-drag-and-drop.min.js"></script> Add an element to your page:
<div class="drop-container">Drag and Drop</div> and initialize Filestack Drag and Drop:
const filestackDnD = new filestackDnD.init('API_KEY', document.querySelector('.drop-container')); That’s it. Now your page element handles the upload by dropping a file on it. Example initialization with filestack Client:
const filestackClient = filestack.init('API_KEY');
const filestackDnD = new filestackDnD.init(filestackClient, document.querySelector('.drop-container')); Example initialization with Options (only image, max size: 1024, max files: 2):
const config = {
  accept: ['image/*'], // default empty array - all files
  maxSize: 1024, // default 0 - no limit
  maxFiles: 2, // default 0 - no limit
  failOverMaxFiles: false, 
}

const filestackDnD = new filestackDnD.init('API_KEY', document.querySelector('.drop-container'), config); Example initialization with sdkConfig:
const sdkConfig = {
  cname: 'cname_test',
  security: {
      policy : 'policy_test',
      signature: 'signature_test'
  }
}

const filestackDnD = new filestackDnD.init('API_KEY', document.querySelector('.drop-container'), null, null, testConfig);

Read More →