Javascript – How to create a drag and drop area to upload files like Gmail

drag and dropfile uploadhtmljavascript

In Gmail, while composing an email, if you drag a file to the compose area (editor) to attach a file, the compose area turns to a drag and drop area to upload files.

I would like to know how to do the following thing:

How to turn an HTML element to a drag and drop file upload area, when
the user drags a file over the element?

It's not necessary to turn the element to drag and drop file upload area. But there can be another HTML element, which works as a drag and drop file upload area and this needs to appear on the top of first element, when user drags the file over first element. So, I need to know how to know when user drags a file and move it over any html element. This is like mouseover event with a dragged file. What is this event?

Best Answer

There is a jQuery plugin that allow you to drag & drop files to be uploaded... Look for jQuery File Upload Demo

To do what you want, you have to add mouseIn event handling, and in the event, do this :

function draggingIn(e) {

   var count =  e.dataTransfer.files.length; (like Chamika Sandamal function)

   if (count > 0) {
        //A file is dragged
        $('droppable').addClass('showBox');
   }
}

For recent browsers, you can use HTML5 draggable capability see here

It looks like this : fiddle I made some tests, just look for drag events