Here's a complete web app that allows users to drop media files into a box and remove them individually or all at once:
<!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: #f4f4f4;
}
.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: #e6f7ff;
border-color: #1890ff;
}
.file-list {
display: flex;
flex-direction: column;
gap: 10px;
}
.file-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px;
border: 1px solid #ddd;
border-radius: 4px;
background-color: #f9f9f9;
}
.file-item button {
background-color: #ff4d4f;
color: white;
border: none;
border-radius: 4px;
padding: 5px 10px;
cursor: pointer;
}
.file-item button:hover {
background-color: #ff7875;
}
.clear-all {
margin-top: 20px;
background-color: #ff4d4f;
color: white;
border: none;
border-radius: 4px;
padding: 10px 20px;
cursor: pointer;
}
.clear-all:hover {
background-color: #ff7875;
}
</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 clearAllButton = 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;
handleFiles(droppedFiles);
});
// Handle files when dropped
function handleFiles(droppedFiles) {
for (let i = 0; i < droppedFiles.length; i++) {
const file = droppedFiles[i];
if (isMediaFile(file)) {
files.push(file);
displayFile(file);
}
}
}
// Check if the file is a media file (image, video, audio)
function isMediaFile(file) {
const mediaTypes = ['image/', 'video/', 'audio/'];
return mediaTypes.some(type => file.type.startsWith(type));
}
// Display a file in the file list
function displayFile(file) {
const fileItem = document.createElement('div');
fileItem.className = 'file-item';
const fileInfo = document.createElement('div');
fileInfo.textContent = `${file.name} (${formatFileSize(file.size)})`;
const removeButton = document.createElement('button');
removeButton.textContent = 'Remove';
removeButton.addEventListener('click', function() {
removeFile(file);
fileItem.remove();
});
fileItem.appendChild(fileInfo);
fileItem.appendChild(removeButton);
fileList.appendChild(fileItem);
}
// Format file size in a human-readable format
function formatFileSize(bytes) {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
}
// Remove a specific file
function removeFile(fileToRemove) {
files = files.filter(file => file !== fileToRemove);
}
// Clear all files
clearAllButton.addEventListener('click', function() {
files = [];
fileList.innerHTML = '';
});
});
</script>
</body>
</html>
This web app includes a drop zone where users can drag and drop media files. The files are displayed in a list with options to remove individual files or clear all files at once. The app is styled with CSS to make it visually appealing and user-friendly. The JavaScript handles the drag and drop events, file validation, and removal functionality.