Drag and Drop File Upload with HTML, CSS & JavaScript
02/15/2021
Contents
Demo
Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width,initial-scale=1">
<title>Drag and Drop File Upload</title>
<link rel="stylesheet" type="text/css" href="https://demo.plantpot.works/assets/css/normalize.css">
<link rel="stylesheet" href="https://use.typekit.net/opg3wle.css">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<div id="container">
<div id="upload-area" class="form">
<div class="upload-ui">
<img src="ic_upload.svg" alt="">
<h2>Drag & Drop Files<br>to Upload</h2>
<p>or</p>
<input id="input-upload" class="input-upload" type="file" name="file" multiple>
<button id="btn-upload" class="btn-upload">Select Files</button>
<div id="file-name" class="file-name"></div>
</div>
</div>
</div>
<script src="dragdrop.js"></script>
</body>
</html>
CSS
@charset "utf-8";
* {
box-sizing: border-box;
margin: 0;
padding: 0;
}
html {
font-size: 16px;
}
body {
background-color: #ace1ff;
font-family: futura-pt, sans-serif;
-webkit-tap-highlight-color: rgba(0,0,0,0);
}
#container {
display: flex;
justify-content: center;
align-items: center;
position: relative;
width: 100%;
height: 100vh;
padding: 10px;
}
.form {
width: 100%;
max-width: 300px;
}
.upload-ui {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
width: 100%;
border: 2px dashed #3d3935;
padding: 20px 0;
}
.upload-ui img {
width: 40px;
}
.upload-ui h2 {
margin-top: 10px;
color: #3d3935;
font-size: 1.5rem;
letter-spacing: 1px;
text-align: center;
}
.upload-ui p {
margin: 8px 0;
font-size: 1.25rem;
}
.input-upload {
display: none;
}
.btn-upload {
width: 200px;
padding: 10px 20px;
border: none;
border-radius: 4px;
background-color: #004b8c;
color: #fff;
font-size: 1.25rem;
outline: none;
cursor: pointer;
}
.file-name {
margin-top: 10px;
font-size: 1rem;
}
.dragover {
background-color: rgba(0, 0, 0, .2);
}
JavaScript
var btn = document.getElementById('btn-upload');
var input = document.getElementById('input-upload');
var uploadArea = document.getElementById('upload-area');
var fileName = document.getElementById('file-name');
btn.addEventListener('click', () => {
input.click();
});
uploadArea.addEventListener('dragover', (event) => {
event.preventDefault();
uploadArea.classList.add('dragover');
});
uploadArea.addEventListener('dragleave', (event) => {
event.preventDefault();
uploadArea.classList.remove('dragover');
});
uploadArea.addEventListener('drop', (event) => {
event.preventDefault();
uploadArea.classList.remove('dragover');
var files = event.dataTransfer.files;
input.files = files;
if(files.length > 1) {
fileName.innerHTML = files.length + "files";
} else {
fileName.innerHTML = files[0].name;
}
});
input.addEventListener('change', (event) => {
event.preventDefault();
var files = input.files;
if(files.length > 1) {
fileName.innerHTML = files.length + "files";
} else {
fileName.innerHTML = files[0].name;
}
});