Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div class="widget-wrap">
  <div id="upload"></div>
  <h1>DRAG-DROP UPLOAD</h1>
 
  <!-- (A) GENERATE UPLOADER HERE -->
  <div id="demo"></div>
  <div>
    * No upload will actually happen.
  </div>
  
  <!-- (X) VISIT CODE-BOXX -->
  <div id="code-boxx">
    Visit
    <a href="https://code-boxx.com/simple-drag-and-drop-file-upload"
       target="_blank">
      Code Boxx
    </a> for more details.
  </div>
</div>
              
            
!

CSS

              
                /* (A) ENTIRE PAGE */
* {
  font-family: arial, sans-serif;
  box-sizing: border-box;
}

/* (B) WRAPPER */
.upwrap { width: 100%; }

/* (C) DRAG-DROP UPLOAD */
/* (C1) DROP ZONE */
.updrop {
  color: #555;
  font-weight: 700;
  text-align: center;
  padding: 50px 0;
  border-radius: 10px;
  border: 2px dashed #c9c9c9;
  background: rgba(255, 255, 255, 0.3);
}
.updrop.highlight {
  background: #ffff45;
  border: 2px dashed #ed2e2e;
}

/* (D) UPLOAD STATS */
.upstat { margin-top: 10px; }
.uprow {
  padding: 15px;
  background: #ededed;
}
.upstat .uprow:nth-child(even) { background: #f5f5f5; }
.upfile { margin-bottom: 5px; }
.upprog, .upbar, .uppercent {
  width: 100%;
  height: 20px;
}
.upprog {
  position: relative;
  background: #fff;
}
.upbar {
  background: #d3e3ff;
  width: 0;
  transition: width 0.5s;
}
.uppercent {
  position: absolute; top: 0; left: 0;
  display: flex; align-items: center; justify-content: center;
}

/* (X) DOES NOT MATTER */
/* PAGE & BODY */
body {
  display: flex;
  align-items: center; justify-content: center;
  min-height: 100vh;
  background-image: url(https://images.unsplash.com/photo-1530569673472-307dc017a82d?crop=entropy&cs=srgb&fm=jpg&ixid=MnwxNDU4OXwwfDF8cmFuZG9tfHx8fHx8fHx8MTY0MzUzMzg1NA&ixlib=rb-1.2.1&q=85);
  background-repeat: no-repeat;
  background-position: center;
  background-size: cover;
}

/* WIDGET */
.widget-wrap {
  min-width: 600px;
  padding: 30px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.4);
}

/* SVG */
#upload {
  width: 100%; height:100px;
  background-image: url('data:image/svg+xml;utf8,<svg viewBox="0 0 640 512" width="100" xmlns="http://www.w3.org/2000/svg"><path d="M537.6 226.6c4.1-10.7 6.4-22.4 6.4-34.6 0-53-43-96-96-96-19.7 0-38.1 6-53.3 16.2C367 64.2 315.3 32 256 32c-88.4 0-160 71.6-160 160 0 2.7.1 5.4.2 8.1C40.2 219.8 0 273.2 0 336c0 79.5 64.5 144 144 144h368c70.7 0 128-57.3 128-128 0-61.9-44-113.6-102.4-125.4zM393.4 288H328v112c0 8.8-7.2 16-16 16h-48c-8.8 0-16-7.2-16-16V288h-65.4c-14.3 0-21.4-17.2-11.3-27.3l105.4-105.4c6.2-6.2 16.4-6.2 22.6 0l105.4 105.4c10.1 10.1 2.9 27.3-11.3 27.3z" /></svg>');
  background-repeat: no-repeat;
  background-position: center;
}

/* FOOTER */
#code-boxx {
  font-weight: 600;
  margin-top: 50px;
}
#code-boxx a {
  display: inline-block;
  padding: 5px;
  text-decoration: none;
  background: #b90a0a;
  color: #fff;
}
#code-boxx , h1 { text-align: center; }
              
            
!

JS

              
                var ddup = {
  // (A) ATTACH DRAG-DROP FILE UPLOADER
  init : (instance) => {
    // (A1) FLAGS + CSS CLASS
    instance.target.classList.add("upwrap");
    instance.upqueue = []; // upload queue
    instance.uplock = false; // uploading in progress

    // (A2) DRAG-DROP HTML INTERFACE
    instance.target.innerHTML =
      `<div class="updrop">Drop Files Here To Upload.</div>
       <div class="upstat"></div>`;
    instance.hzone = instance.target.querySelector(".updrop");
    instance.hstat = instance.target.querySelector(".upstat");

    // (A3) HIGHLIGHT DROP ZONE ON DRAG ENTER
    instance.hzone.ondragenter = (e) => {
      e.preventDefault();
      e.stopPropagation();
      instance.hzone.classList.add("highlight");
    };
    instance.hzone.ondragleave = (e) => {
      e.preventDefault();
      e.stopPropagation();
      instance.hzone.classList.remove("highlight");
    };

    // (A4) DROP TO UPLOAD FILE
    instance.hzone.ondragover = (e) => {
      e.preventDefault();
      e.stopPropagation();
    };
    instance.hzone.ondrop = (e) => {
      e.preventDefault();
      e.stopPropagation();
      instance.hzone.classList.remove("highlight");
      ddup.queue(instance, e.dataTransfer.files);
    };
  },

  // (B) UPLOAD QUEUE
  // * AJAX IS ASYNCHRONOUS, UPLOAD QUEUE PREVENTS SERVER FLOOD
  queue : (instance, files) => {
    // (B1) PUSH FILES INTO QUEUE + GENERATE HTML ROW
    for (let f of files) {
      f.hstat = document.createElement("div");
      f.hstat.className = "uprow";
      f.hstat.innerHTML =
        `<div class="upfile">${f.name} (${f.size} bytes)</div>
         <div class="upprog">
           <div class="upbar"></div>
           <div class="uppercent">0%</div>
         </div>`;
      f.hbar = f.hstat.querySelector(".upbar");
      f.hpercent = f.hstat.querySelector(".uppercent");
      instance.hstat.appendChild(f.hstat);
      instance.upqueue.push(f);
    }

    // (B2) UPLOAD!
    ddup.go(instance);
  },

  // (C) AJAX UPLOAD
  go : (instance) => { if (!instance.uplock && instance.upqueue.length!=0) {
    // (C1) UPLOAD STATUS UPDATE
    instance.uplock = true;

    // (C2) PLUCK OUT FIRST FILE IN QUEUE
    let thisfile = instance.upqueue[0];
    instance.upqueue.shift();

    // (C3) UPLOAD DATA
    let data = new FormData();
    data.append("upfile", thisfile);
    if (instance.data) { for (let [k, v] of Object.entries(instance.data)) {
      data.append(k, v);
    }}

    // (C4) SIMULATED UPLOAD FOR DEMO
    let a = 0, timer = setInterval(() => {
      let r = (25 * a) + "%";
      thisfile.hbar.style.width = r;
      thisfile.hpercent.innerHTML = r;
      if (r=="100%") {
        clearInterval(timer);
        instance.uplock = false;
        ddup.go(instance);
      } else { a++; }
    }, 500);
    
    /* THIS SHOULD ACTUALLY HAPPEN
    // (C4) AJAX UPLOAD
    let xhr = new XMLHttpRequest();
    xhr.open("POST", instance.action);

    // (C5) UPLOAD PROGRESS
    let percent = 0, width = 0;
    xhr.upload.onloadstart = (evt) => {
      thisfile.hbar.style.width = 0;
      thisfile.hpercent.innerHTML = "0%";
    };
    xhr.upload.onloadend = (evt) => {
      thisfile.hbar.style.width = "100%";
      thisfile.hpercent.innerHTML = "100%";
    };
    xhr.upload.onprogress = (evt) => {
      percent = Math.ceil((evt.loaded / evt.total) * 100) + "%";
      thisfile.hbar.style.width = percent;
      thisfile.hpercent.innerHTML = percent;
    };

    // (C6) UPLOAD COMPLETE
    xhr.onload = function () {
      // (C6-1) ERROR
      if (this.response!= "OK" || this.status!=200) {
        thisfile.hpercent.innerHTML = this.response;
      }

      // (C6-2) NEXT BETTER PLAYER!
      else {
        thisfile.hbar.style.width = "100%";
        thisfile.hpercent.innerHTML = "100%";
        instance.uplock = false;
        ddup.go(instance);
      }
    };

    // (C7) GO!
    xhr.send(data);*/
  }}
};

// (D) ATTACH UPLOADER
window.addEventListener("load", () => {
  ddup.init({
    target : document.getElementById("demo") // target HTML <div>
    // action : "dd-upload.php", // server-side upload handler
    // data : { key : "value" } // optional, extra post data
  });
});
              
            
!
999px

Console