-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathindex.html
77 lines (70 loc) · 3.21 KB
/
index.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
<!DOCTYPE html>
<html>
<head>
<title>Socket.IO, File Upload and Message Example</title>
<script src="/socket.io/socket.io.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
var socket = io.connect('https://k810aef055275a.user-app.krampoline.com');
var messageButton = document.getElementById('messageButton');
messageButton.addEventListener('click', function () {
socket.emit('buttonClicked', 'Button was clicked!');
});
var fileList = document.getElementById('fileList');
function fetchFiles() {
fetch('/files')
.then((response) => response.json())
.then((files) => {
fileList.innerHTML = '';
files.forEach((file) => {
var li = document.createElement('li');
li.textContent = file;
fileList.appendChild(li);
});
})
.catch((error) => console.error('Error:', error));
}
var uploadButton = document.getElementById('uploadButton');
uploadButton.addEventListener('click', function () {
var fileInput = document.getElementById('fileInput');
var file = fileInput.files[0];
var formData = new FormData();
formData.append('file', file);
fetch('/upload', {
method: 'POST',
body: formData,
})
.then(() => fetchFiles())
.catch((error) => console.error('Error:', error));
});
fetchFiles();
var loadDataButton = document.getElementById('loadDataButton');
loadDataButton.addEventListener('click', function () {
fetch('/data')
.then((response) => response.json())
.then((data) => {
var dbDataList = document.getElementById('dbDataList');
dbDataList.innerHTML = '';
data.forEach((item) => {
var li = document.createElement('li');
li.textContent = `ID: ${item.id}, Detail: ${item.detail}`;
dbDataList.appendChild(li);
});
})
.catch((error) => console.error('Error:', error));
});
});
</script>
</head>
<body>
<h1>Socket.IO, File Upload and Message Example</h1>
<button id="messageButton">Send Message</button>
<br /><br />
<input type="file" id="fileInput" />
<button id="uploadButton">Upload File</button>
<ul id="fileList"></ul>
<br /><br />
<button id="loadDataButton">Load Data from DB</button>
<ul id="dbDataList"></ul>
</body>
</html>