-
Notifications
You must be signed in to change notification settings - Fork 0
/
upload.html
80 lines (71 loc) · 1.84 KB
/
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
73
74
75
76
77
78
79
80
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Upload and Send File</title>
<style>
body {
font-family: Arial, sans-serif;
}
form {
margin-bottom: 20px;
}
button {
padding: 10px 20px;
background-color: #007bff;
color: #fff;
border: none;
cursor: pointer;
}
button:hover {
background-color: #0056b3;
}
#status {
font-size: 18px;
font-weight: bold;
color: green;
}
</style>
</head>
<body>
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" id="fileInput" name="fileInput" accept=".txt, .pdf, .doc, .docx">
<button type="submit">Upload & Send</button>
</form>
<div id="status"></div>
<script>
const uploadForm = document.getElementById('uploadForm');
const fileInput = document.getElementById('fileInput');
const status = document.getElementById('status');
uploadForm.addEventListener('submit', function(event) {
event.preventDefault();
const formData = new FormData();
formData.append('file', fileInput.files[0]);
fetch('upload.php', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
status.textContent = 'File successfully uploaded and sent!';
} else {
status.textContent = 'Error uploading file. Please try again.';
}
})
.catch(error => {
console.error('Error:', error);
status.textContent = 'An error occurred. Please try again later.';
});
});
// Prevent form submission if file input is empty
uploadForm.addEventListener('submit', function(event) {
if (!fileInput.files.length) {
event.preventDefault();
alert('Please select a file before submitting.');
}
});
</script>
</body>
</html>