-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_upload.html
72 lines (69 loc) · 2.04 KB
/
file_upload.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>File Uploader</title>
<link rel="stylesheet" href="file.css">
</head>
<body>
<div class="wrapper">
<h1>File Uploader</h1>
<div id="file-drop-zone" ondragover="allowDrop(event)" ondrop="drop(event)">
<h2>Browse a file to upload</h2>
<input type="file" id="file" class="file-input" name="file" onchange="handleFiles(this.files)" required>
<label for="file" class="custom-file-upload">Choose File</label>
<h2>Upload file</h2>
<button onclick="uploadFile()">Upload</button>
</div>
<section class="progress-area"></section>
<section class="uploaded-area"></section>
</div>
<script>
function allowDrop(event) {
event.preventDefault();
}
function drop(event) {
event.preventDefault();
const files = event.dataTransfer.files;
handleFiles(files);
}
function handleFiles(files) {
const file = files[0];
const formData = new FormData();
formData.append('file', file);
fetch('/uploadfile', {
method: 'POST',
body: formData
})
.then(response => response.text())
.then(result => {
console.log(result); // Log the result or update UI accordingly
})
.catch(error => {
console.error('Error:', error);
});
}
function uploadFile() {
const inputElement = document.getElementById('file');
const file = inputElement.files[0];
const formData = new FormData();
formData.append('file', file);
fetch('/uploadfile', {
method: 'POST',
body: formData
})
.then(response => {
if (response.ok) {
window.location.href = '/fileuploader'; // Redirect to the fileuploader route after successful upload
} else {
console.error('Error:', response.statusText);
}
})
.catch(error => {
console.error('Error:', error);
});
}
</script>
</body>
</html>