2024-06-13 14:25:35 +02:00
|
|
|
<!DOCTYPE html>
|
|
|
|
<html lang="en">
|
|
|
|
<head>
|
2024-06-13 16:22:59 +02:00
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
|
|
<title>File Upload with Drag and Drop</title>
|
|
|
|
<style>
|
|
|
|
body {
|
|
|
|
font-family: Arial, sans-serif;
|
|
|
|
display: flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
height: 100vh;
|
|
|
|
margin: 0;
|
|
|
|
}
|
|
|
|
.drop-area-container {
|
|
|
|
text-align: center;
|
|
|
|
}
|
|
|
|
.drop-area {
|
|
|
|
width: 300px;
|
|
|
|
height: 200px;
|
|
|
|
border: 2px dashed #ccc;
|
|
|
|
border-radius: 10px;
|
|
|
|
display: inline-flex;
|
|
|
|
justify-content: center;
|
|
|
|
align-items: center;
|
|
|
|
color: #888;
|
|
|
|
font-size: 1.2rem;
|
|
|
|
margin-bottom: 20px;
|
|
|
|
}
|
|
|
|
.drop-area.active {
|
|
|
|
border-color: #38a169;
|
|
|
|
color: #38a169;
|
|
|
|
}
|
|
|
|
.file-input {
|
|
|
|
display: none;
|
|
|
|
}
|
|
|
|
.file-input-label {
|
|
|
|
display: inline-block;
|
|
|
|
padding: 10px 15px;
|
|
|
|
background-color: #007bff;
|
|
|
|
color: #fff;
|
|
|
|
border-radius: 5px;
|
|
|
|
cursor: pointer;
|
|
|
|
margin-right: 10px;
|
|
|
|
}
|
|
|
|
.upload-button {
|
|
|
|
padding: 10px 20px;
|
|
|
|
background-color: #38a169;
|
|
|
|
color: #fff;
|
|
|
|
border: none;
|
|
|
|
border-radius: 5px;
|
|
|
|
cursor: pointer;
|
|
|
|
margin-top: 10px;
|
|
|
|
}
|
|
|
|
.file-list {
|
|
|
|
margin-top: 20px;
|
|
|
|
text-align: left;
|
|
|
|
}
|
|
|
|
.file-item {
|
|
|
|
display: flex;
|
|
|
|
justify-content: space-between;
|
|
|
|
align-items: center;
|
|
|
|
margin-bottom: 5px;
|
|
|
|
padding: 5px;
|
|
|
|
border: 1px solid #ccc;
|
|
|
|
border-radius: 5px;
|
|
|
|
}
|
|
|
|
.cancel-button {
|
|
|
|
background-color: #dc3545;
|
|
|
|
color: #fff;
|
|
|
|
border: none;
|
|
|
|
border-radius: 5px;
|
|
|
|
padding: 5px;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
.progress-bar-container {
|
|
|
|
margin-top: 20px;
|
|
|
|
width: 100%;
|
|
|
|
background-color: #e0e0e0;
|
|
|
|
border-radius: 10px;
|
|
|
|
}
|
|
|
|
.progress-bar {
|
|
|
|
height: 20px;
|
|
|
|
background-color: #38a169;
|
|
|
|
width: 0;
|
|
|
|
max-width: 100%;
|
|
|
|
border-radius: 10px;
|
|
|
|
text-align: center;
|
|
|
|
color: white;
|
|
|
|
line-height: 20px;
|
|
|
|
}
|
|
|
|
</style>
|
2024-06-13 14:25:35 +02:00
|
|
|
</head>
|
|
|
|
<body>
|
2024-06-13 16:22:59 +02:00
|
|
|
<div class="drop-area-container">
|
|
|
|
<div class="drop-area" id="dropArea">
|
|
|
|
<p>Drag & Drop files here</p>
|
|
|
|
</div>
|
|
|
|
<label for="fileInput" class="file-input-label">Select files</label>
|
|
|
|
<input type="file" id="fileInput" class="file-input" multiple>
|
|
|
|
<button id="uploadButton" class="upload-button">Upload</button>
|
|
|
|
|
|
|
|
<div class="file-list" id="fileList"></div>
|
|
|
|
|
|
|
|
<div class="progress-bar-container">
|
|
|
|
<div class="progress-bar" id="progressBar">0%</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<script>
|
|
|
|
const dropArea = document.getElementById('dropArea');
|
|
|
|
const fileInput = document.getElementById('fileInput');
|
|
|
|
const uploadButton = document.getElementById('uploadButton');
|
|
|
|
const fileList = document.getElementById('fileList');
|
|
|
|
const progressBar = document.getElementById('progressBar');
|
|
|
|
let filesToUpload = []; // Array to store files to upload
|
|
|
|
|
|
|
|
// Get the size limit from the URL parameter 'limit'
|
|
|
|
const urlParams = new URLSearchParams(window.location.search);
|
|
|
|
const sizeLimit = parseInt(urlParams.get('limit')) || 10485760; // Default to 10MB if no limit specified
|
|
|
|
|
|
|
|
['dragenter', 'dragover', 'dragleave', 'drop'].forEach(eventName => {
|
|
|
|
dropArea.addEventListener(eventName, preventDefaults, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
function preventDefaults(e) {
|
|
|
|
e.preventDefault();
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
|
|
|
|
|
|
|
['dragenter', 'dragover'].forEach(eventName => {
|
|
|
|
dropArea.addEventListener(eventName, highlight, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
['dragleave', 'drop'].forEach(eventName => {
|
|
|
|
dropArea.addEventListener(eventName, unhighlight, false);
|
|
|
|
});
|
|
|
|
|
|
|
|
function highlight() {
|
|
|
|
dropArea.classList.add('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
function unhighlight() {
|
|
|
|
dropArea.classList.remove('active');
|
|
|
|
}
|
|
|
|
|
|
|
|
dropArea.addEventListener('drop', handleDrop, false);
|
|
|
|
|
|
|
|
function handleDrop(e) {
|
|
|
|
let dt = e.dataTransfer;
|
|
|
|
let files = dt.files;
|
|
|
|
|
|
|
|
addFilesToUpload(files);
|
|
|
|
updateFileList();
|
|
|
|
updateProgressBar();
|
|
|
|
}
|
2024-06-13 14:25:35 +02:00
|
|
|
|
2024-06-13 16:22:59 +02:00
|
|
|
fileInput.addEventListener('change', function(e) {
|
|
|
|
let files = this.files;
|
|
|
|
|
|
|
|
addFilesToUpload(files);
|
|
|
|
updateFileList();
|
|
|
|
updateProgressBar();
|
|
|
|
});
|
|
|
|
|
|
|
|
function addFilesToUpload(newFiles) {
|
|
|
|
filesToUpload = [...filesToUpload, ...newFiles];
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFileList() {
|
|
|
|
fileList.innerHTML = ''; // Clear previous file list
|
|
|
|
|
|
|
|
filesToUpload.forEach((file, index) => {
|
|
|
|
const fileSize = getFileSize(file.size);
|
|
|
|
const listItem = document.createElement('div');
|
|
|
|
listItem.classList.add('file-item');
|
|
|
|
listItem.innerHTML = `
|
|
|
|
<span>${file.name} (${fileSize})</span>
|
|
|
|
<button class="cancel-button" data-index="${index}">Cancel</button>
|
|
|
|
`;
|
|
|
|
fileList.appendChild(listItem);
|
|
|
|
});
|
|
|
|
|
|
|
|
document.querySelectorAll('.cancel-button').forEach(button => {
|
|
|
|
button.addEventListener('click', removeFile, false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function removeFile(e) {
|
|
|
|
const index = e.target.dataset.index;
|
|
|
|
filesToUpload.splice(index, 1);
|
|
|
|
updateFileList();
|
|
|
|
updateProgressBar();
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateProgressBar() {
|
|
|
|
const totalSize = filesToUpload.reduce((acc, file) => acc + file.size, 0);
|
|
|
|
const usedPercentage = (totalSize / sizeLimit) * 100;
|
|
|
|
progressBar.style.width = `${Math.min(usedPercentage, 100)}%`;
|
|
|
|
progressBar.textContent = `${Math.min(usedPercentage, 100).toFixed(2)}%`;
|
|
|
|
|
|
|
|
if (usedPercentage > 100) {
|
|
|
|
progressBar.style.backgroundColor = '#dc3545'; // Red if over limit
|
|
|
|
alert('Total file size exceeds the limit!');
|
|
|
|
} else {
|
|
|
|
progressBar.style.backgroundColor = '#38a169'; // Green if within limit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function getFileSize(size) {
|
|
|
|
if (size === 0) return '0 Bytes';
|
|
|
|
const k = 1024;
|
|
|
|
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
|
|
|
|
const i = Math.floor(Math.log(size) / Math.log(k));
|
|
|
|
return parseFloat((size / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
uploadButton.addEventListener('click', function() {
|
|
|
|
if (filesToUpload.length > 0) {
|
|
|
|
const totalSize = filesToUpload.reduce((acc, file) => acc + file.size, 0);
|
|
|
|
if (totalSize > sizeLimit) {
|
|
|
|
alert('Total file size exceeds the limit! Please remove some files.');
|
|
|
|
} else {
|
|
|
|
// Here you can send filesToUpload to the server or perform other actions
|
|
|
|
console.log('Uploading files:', filesToUpload);
|
|
|
|
alert('Files uploaded successfully!');
|
|
|
|
// Clear the filesToUpload array and update the file list display
|
|
|
|
filesToUpload = [];
|
|
|
|
updateFileList();
|
|
|
|
updateProgressBar();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
alert('Please select files to upload.');
|
2024-06-13 14:25:35 +02:00
|
|
|
}
|
2024-06-13 16:22:59 +02:00
|
|
|
});
|
|
|
|
</script>
|
|
|
|
|
2024-06-13 14:25:35 +02:00
|
|
|
</body>
|
2024-06-13 16:22:59 +02:00
|
|
|
</html>
|