Certainly! Below is a complete HTML, CSS, and JavaScript code that creates an attractive and dynamic drag & drop UI as per your specifications. This UI allows users to upload Excel (.xlsx, .xls) and CSV files either by clicking on the drag zone or by dragging and dropping the files anywhere on the screen. After uploading, it gets ready for further cleaning or formatting.
HTML Code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Drag & Drop File Upload</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div id="drop-zone">
<p>Drag anywhere on the screen or click here to upload files</p>
<input type="file" id="file-input" accept=".xlsx, .xls, .csv" multiple>
</div>
<script src="script.js"></script>
</body>
</html>
CSS Code (styles.css)
body {
margin: 0;
padding: 0;
font-family: Arial, sans-serif;
}
#drop-zone {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(255, 255, 255, 0.9);
border: 3px dashed #aaa;
display: flex;
justify-content: center;
align-items: center;
text-align: center;
cursor: pointer;
transition: background-color 0.3s;
}
#drop-zone p {
font-size: 24px;
color: #555;
}
#drop-zone.hover {
background-color: rgba(220, 220, 220, 0.9);
}
#file-input {
display: none;
}
JavaScript Code (script.js)
// Get references to the drop zone and file input
const dropZone = document.getElementById('drop-zone');
const fileInput = document.getElementById('file-input');
// Add click event to the drop zone to trigger file selection
dropZone.addEventListener('click', () => {
fileInput.click();
});
// Add change event to handle selected files
fileInput.addEventListener('change', (e) => {
handleFiles(e.target.files);
});
// Handle drag over event to allow dropping
window.addEventListener('dragover', (e) => {
e.preventDefault();
});
// Highlight drop zone when file is dragged over
window.addEventListener('dragenter', () => {
dropZone.classList.add('hover');
});
// Remove highlight when drag leaves
window.addEventListener('dragleave', () => {
dropZone.classList.remove('hover');
});
// Handle drop event
window.addEventListener('drop', (e) => {
e.preventDefault();
dropZone.classList.remove('hover');
const files = e.dataTransfer.files;
handleFiles(files);
});
// Function to handle the files
function handleFiles(files) {
const validExtensions = ['xlsx', 'xls', 'csv'];
const fileArray = Array.from(files);
const validFiles = fileArray.filter(file => {
const fileExtension = file.name.split('.').pop().toLowerCase();
return validExtensions.includes(fileExtension);
});
if (validFiles.length > 0) {
// Proceed with further processing (cleaning or formatting)
alert(`${validFiles.length} file(s) ready for cleaning or formatting.`);
// Your code to handle the files goes here...
} else {
alert('Please upload Excel or CSV files only.');
}
}
Explanation
-
HTML:
- The
div with id="drop-zone" acts as the area that users can click to select files or where they can drop files.
- The
input with type="file" is hidden but is triggered when the drop zone is clicked.
- The
accept attribute restricts file types to Excel and CSV files.
-
CSS:
- The
#drop-zone is styled to cover the entire viewport with a white background and dashed borders.
- The text is centered both horizontally and vertically.
- The
.hover class changes the background color when a file is dragged over the window.
-
JavaScript:
- Event listeners are added to handle clicking, dragging, and dropping files.
- The
handleFiles function filters out any files that are not Excel or CSV files and alerts the user.
- Placeholder alert is used to indicate readiness for further processing; you can replace it with actual processing code.
How to Use
-
Create three files in the same directory:
index.html (HTML code)
styles.css (CSS code)
script.js (JavaScript code)
-
Copy the respective code into each file.
-
Open index.html in a web browser.
-
Test the functionality by:
- Clicking on the drag zone to select files.
- Dragging and dropping files anywhere on the screen.
-
Ensure you select or drop only Excel or CSV files to see the alert indicating readiness for further processing.
Customization
-
Enhance the Appearance:
- You can add background images, gradients, or more elaborate styles to make the UI more attractive.
-
Processing Files:
- Replace the
alert in the handleFiles function with your actual file processing logic (e.g., reading the files, displaying data, etc.).
-
Multiple Files:
- The
multiple attribute in the file input allows multiple file uploads; remove it if only one file should be uploaded at a time.
Browser Compatibility
- This code uses standard web APIs and should work in all modern browsers.
- Ensure JavaScript is enabled in the browser settings.
Feel free to modify and enhance the code to suit your specific requirements. If you have any questions or need further assistance, please let me know!