Certainly! Below is a simple example of a web app that allows users to drag and drop media files into a box. Users can also remove individual files or clear all files. This example uses HTML, CSS, and JavaScript.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Drag and Drop File Upload</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="container">
<h1>Drag and Drop Files</h1>
<div id="drop-area" class="drop-area">
<p>Drag & Drop files here or click to select files</p>
</div>
<div id="file-list" class="file-list">
<button id="clear-all" class="clear-all">Clear All</button>
</div>
</div>
<script src="script.js"></script>
</body>
</html>
styles.css
body {
font-family: Arial, sans-serif;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #f4f4f9;
}
.container {
text-align: center;
background: white;
padding: 20px;
border-radius: 10px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
width: 400px;
}
.drop-area {
border: 2px dashed #ccc;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
cursor: pointer;
transition: background 0.3s ease;
}
.drop-area.highlight {
border-color: #4caf50;
background: #e8f5e9;
}
.file-list {
list-style: none;
padding: 0;
}
.file-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 5px 0;
border-bottom: 1px solid #ccc;
}
.file-item:last-child {
border-bottom: none;
}
.remove-btn {
background: #f44336;
color: white;
border: none;
border-radius: 5px;
padding: 5px 10px;
cursor: pointer;
}
.remove-btn:hover {
background: #d32f2f;
}
.clear-all {
background: #2196F3;
color: white;
border: none;
border-radius: 5px;
padding: 10px;
cursor: pointer;
margin-bottom: 10px;
width: 100%;
}
.clear-all:hover {
background: #1976d2
}
script.js
document.addEventListener("DOMContentLoaded", () => {
const dropArea = document.getElementById('drop-area');
const fileList = document.getElementById('file-list');
const clearAllBtn = document.getElementById('clear-all');
let files = [];
// Prevent default drag behaviours
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, preventDefaults, false)
document.body.addEventListener(eventName, preventDefaults, false)
});
// Highlight drop area when item is dragged over it
['dragenter', 'dragover'].forEach(eventName => {
dropArea.addEventListener(eventName, highlight, false)
});
['dragleave', 'drop'].forEach(eventName => {
dropArea.addEventListener(eventName, unhighlight, false)
});
// Handle dropped files
dropArea.addEventListener('drop', handleDrop, false)
dropArea.addEventListener('click', () => {
document.querySelector('#fileInput').click();
})
const fileInput = document.createElement('input');
fileInput.id = 'fileInput';
fileInput.type = 'file';
fileInput.multiple = true;
fileInput.style.display = 'none';
document.body.appendChild(fileInput);
fileInput.addEventListener('change', (e) => {
handleDrop({dataTransfer: {files: e.target.files}})
fileInput.value = ""; // reset input
})
function preventDefaults (e) {
e.preventDefault()
e.stopPropagation()
}
function highlight(e) {
dropArea.classList.add('highlight')
}
function unhighlight(e) {
dropArea.classList.remove('highlight')
}
function handleDrop(e) {
let dt = e.dataTransfer ? e.dataTransfer : e;
let newFiles = dt.files;
for (let i = 0; i < newFiles.length; i++) {
if (!files.some(file => file.name === newFiles[i].name)) {
files.push(newFiles[i]);
renderFile(newFiles[i]);
}
}
}
function renderFile(file) {
const fileItem = document.createElement('div');
fileItem.classList.add('file-item');
const fileName = document.createElement('span');
fileName.textContent = file.name;
fileName.title = file.name;
const removeBtn = document.createElement('button');
removeBtn.classList.add('remove-btn');
removeBtn.textContent = 'Remove';
removeBtn.addEventListener('click', () => removeFile(file.name));
fileItem.appendChild(fileName);
fileItem.appendChild(removeBtn);
fileList.appendChild(fileItem);
}
function removeFile(fileName) {
files = files.filter(file => file.name !== fileName);
const fileItem = Array.from(fileList.children).find(child => child.firstChild.title === fileName);
if (fileItem) fileList.removeChild(fileItem);
}
clearAllBtn.addEventListener('click', () => {
files = [];
while (fileList.firstChild) {
fileList.removeChild(fileList.firstChild);
}
});
});
Explanation:
-
index.html:
- Contains the basic structure of the web app.
- Includes a
drop-area div where users can drag and drop files.
- Includes a
file-list div to display the list of files.
- Includes a hidden file input to handle click events.
-
styles.css:
- Provides styling for the container, drop area, file list, and buttons.
- Adds hover effects for buttons.
-
script.js:
- Adds event listeners for drag and drop functionality.
- Highlights the drop area when files are dragged over it.
- Handles the dropping of files and adds them to the file list.
- Allows users to remove individual files.
- Allows users to clear all files.
- Adds an invisible file input to handle click events and file selection.
To run this web app, save the code in three separate files (index.html, styles.css, and script.js) in the same directory and open index.html in a web browser.