Generate a single index.html file that implements a drag-and-drop file upload interface…
- User
Generate a single index.html file that implements a drag-and-drop file upload interface with visual feedback.
The file should:
Contain a designated area (e.g., a styled div) where users can drag and drop files from their local machine. Handle the browser's native drag-and-drop events (dragover, dragleave, drop) to visually indicate the drop zone is active when files are dragged over it. When files are dropped, prevent the default browser behavior (opening the file). For each file dropped, dynamically add an element to the page displaying: The file name. The file size. A simulated upload progress bar or indicator that visually updates over a few seconds (e.g., 3-5 seconds per file) to completion, without actually uploading the file anywhere. Optionally include the Tailwind CSS CDN link for styling the drop zone, file list, and progress bars, but the core functionality and visual feedback must work with basic or embedded custom CSS. External JavaScript libraries via CDN are allowed if they significantly simplify handling drag-and-drop files or progress simulation, but vanilla JavaScript and the browser's File API are sufficient and preferred if possible. All HTML structure, necessary CSS for layout and appearance (custom or via optional Tailwind), and the JavaScript code for handling drag-and-drop events, processing file data, dynamically updating the DOM, and simulating progress must be entirely self-contained within this single index.html file. This is harder as it involves browser file APIs and dynamic UI updates based on user interaction.
Assistant
Here's a self-contained index.html file that implements the drag-and-drop file upload interface with all the requested features:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>File Upload Interface</title> <style> * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; padding: 2rem; background-color: #f5f5f5; } .drop-zone { width: 100%; max-width: 600px; height: 200px; margin: 0 auto; padding: 25px; display: flex; align-items: center; justify-content: center; text-align: center; border: 2px dashed #4a5568; border-radius: 10px; background-color: white; transition: all 0.3s ease; cursor: pointer; } .drop-zone.dragover { background-color: #e2e8f0; border-color: #2b6cb0; } .file-list { max-width: 600px; margin: 2rem auto; } .file-item { background-color: white; padding: 1rem; margin-bottom: 1rem; border-radius: 8px; box-shadow: 0 2px 4px rgba(0,0,0,0.1); } .file-info { display: flex; justify-content: space-between; margin-bottom: 0.5rem; } .progress-bar { width: 100%; height: 8px; background-color: #e2e8f0; border-radius: 4px; overflow: hidden; } .progress { width: 0%; height: 100%; background-color: #4299e1; transition: width 0.3s ease; } .file-size { color: #718096; font-size: 0.875rem; } </style> </head> <body> <div class="drop-zone" id="dropZone"> <div class="drop-zone-text"> Drag and drop files here or click to upload </div> </div> <div class="file-list" id="fileList"></div> <script> const dropZone = document.getElementById('dropZone'); const fileList = document.getElementById('fileList'); // Prevent default drag behaviors ['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => { dropZone.addEventListener(eventName, preventDefaults, false); document.body.addEventListener(eventName, preventDefaults, false); }); function preventDefaults (e) { e.preventDefault(); e.stopPropagation(); } // Highlight drop zone when item is dragged over it ['dragenter', 'dragover'].forEach(eventName => { dropZone.addEventListener(eventName, highlight, false); }); ['dragleave', 'drop'].forEach(eventName => { dropZone.addEventListener(eventName, unhighlight, false); }); function highlight(e) { dropZone.classList.add('dragover'); } function unhighlight(e) { dropZone.classList.remove('dragover'); } // Handle dropped files dropZone.addEventListener('drop', handleDrop, false); function handleDrop(e) { const dt = e.dataTransfer; const files = dt.files; handleFiles(files); } function handleFiles(files) { [...files].forEach(uploadFile); } 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]; } function uploadFile(file) { const fileItem = document.createElement('div'); fileItem.className = 'file-item'; const fileInfo = document.createElement('div'); fileInfo.className = 'file-info'; const fileName = document.createElement('div'); fileName.textContent = file.name; const fileSize = document.createElement('div'); fileSize.className = 'file-size'; fileSize.textContent = formatFileSize(file.size); fileInfo.appendChild(fileName); fileInfo.appendChild(fileSize); const progressBar = document.createElement('div'); progressBar.className = 'progress-bar'; const progress = document.createElement('div'); progress.className = 'progress'; progressBar.appendChild(progress); fileItem.appendChild(fileInfo); fileItem.appendChild(progressBar); fileList.appendChild(fileItem); // Simulate upload progress let width = 0; const duration = 3000; // 3 seconds const interval = 30; // Update every 30ms const steps = duration / interval; const increment = 100 / steps; const simulateProgress = setInterval(() => { width += increment; progress.style.width = `${width}%`; if (width >= 100) { clearInterval(simulateProgress); } }, interval); } // Allow clicking the drop zone to trigger file input dropZone.addEventListener('click', () => { const input = document.createElement('input'); input.type = 'file'; input.multiple = true; input.onchange = e => { handleFiles(e.target.files); }; input.click(); }); </script> </body> </html>This implementation includes:
- A styled drop zone that provides visual feedback when files are dragged over it
- Handling of all drag-and-drop events with prevention of default browser behavior
- File size formatting in appropriate units (bytes, KB, MB, GB)
- A clean, modern design with subtle animations and visual feedback
- Simulated upload progress bars that complete in 3 seconds
- Support for multiple file uploads
- Click-to-upload functionality as an alternative to drag-and-drop
- Responsive design that works on different screen sizes
The file is completely self-contained and doesn't require any external dependencies. It uses custom CSS for styling and vanilla JavaScript for all functionality. The progress bars are simulated using setInterval to create a smooth animation effect.
To use it, simply save it as index.html and open it in a web browser. You can drag and drop files onto the designated area or click to select files through the system file picker.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.