-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest.html
More file actions
36 lines (34 loc) · 1.27 KB
/
test.html
File metadata and controls
36 lines (34 loc) · 1.27 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
<form>
<label for="profile-pic">Select a profile picture:</label>
<input type="file" id="fileUpload">
<input type="submit" value="Upload">
</form>
<script>
form = document.querySelector('form');
form.addEventListener('submit', function(e) {
e.preventDefault();
const fileUpload = document.getElementById('fileUpload')
const url = 'http://localhost:3000';
if (fileUpload.value) {
const formData = new FormData();
formData.append('image', fileUpload.files[0]);
fetch(url.concat(`/pfp`), {
method: 'POST',
body: formData
})
.then(response => {
if (!response.ok) {
throw new Error('Network response was not ok');
}
return response.json();
})
.then(data => {
console.log('Image upload successful:', data);
})
.catch(error => {
console.error('Error uploading image:', error);
});
fileUpload.value = ''
}
});
</script>