-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupload.html
More file actions
160 lines (142 loc) · 5.44 KB
/
Copy pathupload.html
File metadata and controls
160 lines (142 loc) · 5.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
---
---
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="general.css">
<meta charset="UTF-8">
<title>File Upload</title>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f4f4f9;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
height: 100vh;
}
.container {
background-color: #fff;
padding: 20px;
border-radius: 8px;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
max-width: 400px;
width: 100%;
margin-top: 20px;
}
form {
display: flex;
flex-direction: column;
}
label {
margin-bottom: 5px;
font-weight: bold;
}
input[type="text"], input[type="file"] {
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
button {
padding: 10px;
border: none;
border-radius: 4px;
background-color: #5cb85c;
color: white;
font-size: 16px;
cursor: pointer;
}
button:hover {
background-color: #4cae4c;
}
</style>
</head>
<body>
{% include header2.html %}
<main class="container">
<form id="uploadForm">
<select id="assignmentDropdown">
<option value="" disabled selected>Select Assignment</option>
</select>
<p><span id="selectedAssignmentName" hidden></span></p>
<label for="fileInput">Upload File:</label>
<input type="file" id="fileInput" name="file">
<button type="button" onclick="uploadFile()">Upload</button>
</form>
</main>
<script>
async function uploadFile() {
const url = "https://schaal.stu.nighthawkcodingsociety.com/";
// const url = "https://schaal.stu.nighthawkcodingsociety.com";
const fileInput = document.getElementById('fileInput');
let nameInput = localStorage.getItem('username') !== "" ? localStorage.getItem('username') : "Name Not Provided";
const assignmentDropdown = document.getElementById('assignmentDropdown');
if (assignmentDropdown.value === "") {
alert("Please select an assignment.");
return;
}
const selectedIndex = assignmentDropdown.selectedIndex;
const selectedOption = assignmentDropdown.options[selectedIndex];
const assignmentName = selectedOption.textContent;
document.getElementById('selectedAssignmentName').textContent = assignmentName;
const formData = new FormData();
formData.append('file', fileInput.files[0]);
formData.append('name', nameInput);
formData.append('assignment', assignmentName); // Append assignmentName here
try {
const response = await fetch(url + 'api/fileupload/upload', {
method: 'POST',
body: formData
});
if (response.ok) {
alert('File uploaded successfully');
} else {
alert('File upload failed');
}
} catch (error) {
console.error('Error uploading file:', error);
alert('Error uploading file');
}
}
function fetchAssignments() {
const url = "https://schaal.stu.nighthawkcodingsociety.com/";
// const url = "https://schaal.stu.nighthawkcodingsociety.com";
fetch(url + 'api/assignments/')
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
const dropdown = document.getElementById('assignmentDropdown');
dropdown.innerHTML = '<option value="" disabled selected>Select Assignment</option>';
if (!Array.isArray(data)) {
data = Object.values(data);
}
data.forEach(assignment => {
const option = document.createElement('option');
option.value = assignment.id;
option.textContent = assignment.title;
dropdown.appendChild(option);
});
})
.catch(error => {
console.error('Error fetching assignment data:', error);
});
}
// Fetch assignments on page load
fetchAssignments();
// Event listener for assignment dropdown change
// Event listener for assignment dropdown change
document.getElementById('assignmentDropdown').addEventListener('change', function () {
const selectedIndex = this.selectedIndex;
const selectedOption = this.options[selectedIndex];
const assignmentName = selectedOption.textContent;
document.getElementById('selectedAssignmentName').textContent = assignmentName;
});
</script>
</body>
</html>