Skip to content
Snippets Groups Projects
Commit 1158e9c4 authored by MHEN2606's avatar MHEN2606
Browse files

feat: File Size and Type Checking

parent 1f6df147
Branches
No related merge requests found
...@@ -14,6 +14,7 @@ const render = () => { ...@@ -14,6 +14,7 @@ const render = () => {
xhr.send() xhr.send()
} }
const notificationElement = document.getElementById('notification');
const title = document.getElementById("assignment_name"); const title = document.getElementById("assignment_name");
const description = document.getElementById("description"); const description = document.getElementById("description");
const submitForm = () => { const submitForm = () => {
...@@ -23,15 +24,30 @@ const submitForm = () => { ...@@ -23,15 +24,30 @@ const submitForm = () => {
// Get the file input element // Get the file input element
const fileInput = document.getElementById('file'); 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 // Append data to the FormData object
formData.append("uid", uid); 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.open("POST", `http://localhost:5001/api/files/scholarship/${sid}/assignment/${aid}`);
xhr.onload = () => { xhr.onload = () => {
const res = JSON.parse(xhr.response); const res = JSON.parse(xhr.response);
const notificationElement = document.getElementById('notification');
if (res.status === 'success') { if (res.status === 'success') {
notificationElement.classList.add('success'); notificationElement.classList.add('success');
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment