From 1158e9c40e0b4c84275c4cf7551a012bf76e9e2a Mon Sep 17 00:00:00 2001 From: MHEN2606 <matthew.mahendra@gmail.com> Date: Wed, 15 Nov 2023 09:30:23 +0700 Subject: [PATCH] feat: File Size and Type Checking --- public/js/submit.js | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/public/js/submit.js b/public/js/submit.js index 0eaa905..65854cf 100644 --- a/public/js/submit.js +++ b/public/js/submit.js @@ -14,6 +14,7 @@ const render = () => { xhr.send() } +const notificationElement = document.getElementById('notification'); const title = document.getElementById("assignment_name"); const description = document.getElementById("description"); const submitForm = () => { @@ -23,15 +24,30 @@ const submitForm = () => { // Get the file input element const fileInput = document.getElementById('file'); + // Checking File Types + const allowedFileTypes = ['application/pdf', 'video/mp4']; + if (!allowedFileTypes.includes(fileInput.files[0].type)) { + notificationElement.classList.add('error'); + notificationElement.innerHTML = '<i class="fas fa-times-circle"></i> Hanya File MP4 dan PDF yang diperbolehkan!'; + return false; + } + + // Check file size (in bytes) + const maxSize = 1024 * 1024; // 1 MB + if (fileInput.files[0].size > maxSize) { + notificationElement.classList.add('error'); + notificationElement.innerHTML = '<i class="fas fa-times-circle"></i>Ukuran file maksimal 1MB'; + return false; + } + // Append data to the FormData object formData.append("uid", uid); - formData.append("file", fileInput.files[0]); // Assuming fileInput is your file input element + formData.append("file", fileInput.files[0]); xhr.open("POST", `http://localhost:5001/api/files/scholarship/${sid}/assignment/${aid}`); xhr.onload = () => { const res = JSON.parse(xhr.response); - const notificationElement = document.getElementById('notification'); if (res.status === 'success') { notificationElement.classList.add('success'); -- GitLab