Drag and drop files in a drop zone example

Drag and drop files to a drop zone, display file details in a list

Example of drag'n'drop of a file

Try the example below directly in your browser (just drag and drop files to the greyish drop zone), or play with it at CodePen:

Complete source code from the example:

  1. <!DOCTYPE html>
  2. <html lang=“en”>
  3. <head>
  4.    <style>
  5.       div {
  6.          height: 150px;
  7.          width: 350px;
  8.          float: left;
  9.          border: 2px solid #666666;
  10.          backgroundcolor: #ccc;
  11.          marginright: 5px;
  12.          borderradius: 10px;
  13.          boxshadow: inset 0 0 3px #000;
  14.          textalign: center;
  15.          cursor: move;
  16.       }
  17.       .dragged {
  18.          border: 2px dashed #000;
  19.          backgroundcolor: green;
  20.       }
  21.       .draggedOver {
  22.          border: 2px dashed #000;
  23.          backgroundcolor: green;
  24.       }
  25.     </style>
  26.     
  27.       function dragLeaveHandler(event) {
  28.           console.log(“drag leave”);
  29.           // Set style of drop zone to default
  30.           event.target.classList.remove(‘draggedOver’);
  31.       }
  32.       function dragEnterHandler(event) {
  33.           console.log(“Drag enter”);
  34.           // Show some visual feedback
  35.           event.target.classList.add(‘draggedOver’);
  36.       }
  37.       function dragOverHandler(event) {
  38.           //console.log(“Drag over a droppable zone”);
  39.           // Do not propagate the event
  40.           event.stopPropagation();
  41.           // Prevent default behavior, in particular when we drop images or links
  42.           event.preventDefault();
  43.       }
  44.       function dropHandler(event) {
  45.           console.log(‘drop event’);
  46.           // Do not propagate the event
  47.           event.stopPropagation();
  48.           // Prevent default behavior, in particular when we drop images or links
  49.           event.preventDefault();
  50.           // reset the visual look of the drop zone to default
  51.           event.target.classList.remove(‘draggedOver’);
  52.           // get the files from the clipboard
  53.           var files = event.dataTransfer.files;
  54.           var filesLen = files.length;
  55.           var filenames = “”;
  56.           // iterate on the files, get details using the file API
  57.           // Display file names in a list.
  58.           for(var i = 0 ; i  filesLen ; i++) {
  59.               filenames += ‘\n’ + files[i].name;
  60.               // Create a li, set its value to a file name, add it to the ol
  61.               var li = document.createElement(‘li’);
  62.               li.textContent = files[i].name; document.querySelector(“#droppedFiles”).appendChild(li);
  63.           }
  64.           console.log(files.length + ‘ file(s) have been dropped:\n’ + filenames);
  65.       }
  66.   
  67. </head>
  68. <body>
  69.   <h2>Drop your files here!</h2>
  70.   
     id=“droppableZone” ondragenter=dragEnterHandler(event) ondrop=dropHandler(event)     
  71.                           ondragover=dragOverHandler(event)   ondragleave=dragLeaveHandler(event)>
  72.      Drop zone
  73.      
       id=“droppedFiles”>
  74.  
  • <body>
  • <html>
  • Note that:

      • We prevented the browser default behavior in the drop and dragover handlers Otherwise, if we dropped a media file (an image, an audio of video file), the browser would try to display/play it in a new window/tab. We also stop the  propagation for performance reasons, because when we drag an object it can raise many events within the parents of the drop zone element as well.
      • Lines 73-74 create a <li> element. Its value is initialized with the file name of the current file in the collection, and added to the <ol> list.

    In principle, this example is very similar to the “fruit” examples we worked through earlier, except that this time we’re working with files. And when we work with files, it is important to prevent the browser’s default behavior.