Here's a complete web app that allows users to drop media files into a box and remove individual files or all files:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Media File Drop Zone</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f0f0f0;
}
.container {
max-width: 800px;
margin: 0 auto;
background-color: white;
padding: 20px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
.drop-zone {
border: 2px dashed #ccc;
border-radius: 8px;
padding: 20px;
text-align: center;
margin-bottom: 20px;
transition: background-color 0.3s;
}
.drop-zone.highlight {
background-color: #f0f0f0;
border-color: #007bff;
}
.file-list {
display: flex;
flex-wrap: wrap;
gap: 10px;
margin-bottom: 20px;
}
.file-item {
position: relative;
width: 100px;
height: 100px;
border: 1px solid #ccc;
border-radius: 4px;
overflow: hidden;
display: flex;
align-items: center;
justify-content: center;
}
.file-item img, .file-item video, .file-item audio {
max-width: 100%;
max-height: 100%;
}
.file-item .remove-btn {
position: absolute;
top: 5px;
right: 5px;
background-color: red;
color: white;
border: none;
border-radius: 50%;
width: 20px;
height: 20px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
}
.clear-all {
background-color: #dc3545;
color: white;
border: none;
padding: 10px 20px;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
}
.clear-all:hover {
background-color: #c82333;
}
</style>
</head>
<body>
<div class="container">
<h1>Media File Drop Zone</h1>
<div id="dropZone" class="drop-zone">
<p>Drag and drop media files here</p>
</div>
<div id="fileList" class="file-list">
<!-- Files will be displayed here -->
</div>
<button id="clearAll" class="clear-all">Clear All</button>
</div>
<script>
document.addEventListener('DOMContentLoaded', function() {
const dropZone = document.getElementById('dropZone');
const fileList = document.getElementById('fileList');
const clearAllBtn = document.getElementById('clearAll');
let files = [];
// Prevent default drag behaviors
dropZone.addEventListener('dragover', function(e) {
e.preventDefault();
dropZone.classList.add('highlight');
});
dropZone.addEventListener('dragleave', function() {
dropZone.classList.remove('highlight');
});
dropZone.addEventListener('drop', function(e) {
e.preventDefault();
dropZone.classList.remove('highlight');
const droppedFiles = e.dataTransfer.files;
for (let i = 0; i < droppedFiles.length; i++) {
const file = droppedFiles[i];
if (isMediaFile(file)) {
files.push(file);
displayFile(file);
}
}
});
// Function to check if the file is a media file
function isMediaFile(file) {
const mediaTypes = ['image/', 'video/', 'audio/'];
return mediaTypes.some(type => file.type.startsWith(type));
}
// Function to display a file
function displayFile(file) {
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
const fileURL = URL.createObjectURL(file);
if (file.type.startsWith('image/')) {
const img = document.createElement('img');
img.src = fileURL;
fileItem.appendChild(img);
} else if (file.type.startsWith('video/')) {
const video = document.createElement('video');
video.src = fileURL;
video.controls = true;
fileItem.appendChild(video);
} else if (file.type.startsWith('audio/')) {
const audio = document.createElement('audio');
audio.src = fileURL;
audio.controls = true;
fileItem.appendChild(audio);
}
const removeBtn = document.createElement('button');
removeBtn.className = 'remove-btn';
removeBtn.innerHTML = '×';
removeBtn.addEventListener('click', function() {
// Remove the file from the files array
files = files.filter(f => f !== file);
// Remove the file item from the DOM
fileList.removeChild(fileItem);
// Revoke the object URL to free memory
URL.revokeObjectURL(fileURL);
});
fileItem.appendChild(removeBtn);
fileList.appendChild(fileItem);
}
// Clear all files
clearAllBtn.addEventListener('click', function() {
files.forEach(file => {
const fileURL = URL.createObjectURL(file);
URL.revokeObjectURL(fileURL);
});
files = [];
fileList.innerHTML = '';
});
});
</script>
</body>
</html>
This web app includes a drop zone where users can drag and drop media files (images, videos, audio). Each file is displayed as a thumbnail with a remove button. There's also a "Clear All" button to remove all files at once. The app is styled with CSS for a clean and user-friendly interface.