Also added 500MB Limit

This commit is contained in:
johnnyq
2023-08-23 16:51:10 -04:00
parent 1083ac88d9
commit 03e6e31f8c

View File

@@ -28,25 +28,36 @@
<script> <script>
const maxFiles = 20; // Set the maximum number of allowed files const maxFiles = 20; // Set the maximum number of allowed files
const maxTotalSize = 500 * 1024 * 1024; // 500MB in bytes
const fileInput = document.getElementById('fileInput'); const fileInput = document.getElementById('fileInput');
const uploadForm = document.getElementById('uploadForm'); const uploadForm = document.getElementById('uploadForm');
fileInput.addEventListener('change', function () { fileInput.addEventListener('change', function () {
if (fileInput.files.length > maxFiles) { const totalSize = calculateTotalFileSize(fileInput.files);
alert(`You can only upload up to ${maxFiles} files at a time.`); if (fileInput.files.length > maxFiles || totalSize > maxTotalSize) {
alert(`You can only upload up to ${maxFiles} files at a time and the total file size must not exceed 500MB.`);
resetFileInput(); resetFileInput();
} }
}); });
uploadForm.addEventListener('submit', function (event) { uploadForm.addEventListener('submit', function (event) {
if (fileInput.files.length > maxFiles) { const totalSize = calculateTotalFileSize(fileInput.files);
if (fileInput.files.length > maxFiles || totalSize > maxTotalSize) {
event.preventDefault(); event.preventDefault();
alert(`You can only upload up to ${maxFiles} files at a time.`); alert(`You can only upload up to ${maxFiles} files at a time and the total file size must not exceed 500MB.`);
resetFileInput(); resetFileInput();
} }
}); });
function calculateTotalFileSize(files) {
let totalSize = 0;
for (const file of files) {
totalSize += file.size;
}
return totalSize;
}
function resetFileInput() { function resetFileInput() {
fileInput.value = ''; // Clear the selected files fileInput.value = ''; // Clear the selected files
} }