-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
73 lines (61 loc) · 1.72 KB
/
Copy pathindex.html
File metadata and controls
73 lines (61 loc) · 1.72 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Node.js File Uploader</title>
<style>
body {
font-family: Arial, Helvetica, sans-serif;
font-size: 1.5rem;
background-color: #333;
color: whitesmoke;
}
input,
button {
font: inherit;
width: max-content;
}
form {
display: flex;
flex-flow: column nowrap;
gap: 1.5rem;
}
</style>
</head>
<body>
<h1>Node.js File Uploader</h1>
<form id="uploadForm">
<input type="file" id="myFiles" accept="image/*" multiple />
<button>Submit</button>
</form>
<h2></h2>
<h3></h3>
<script>
const form = document.getElementById('uploadForm');
const sendFiles = async () => {
// Object
const myFiles = document.getElementById('myFiles').files;
const formData = new FormData();
Object.keys(myFiles).forEach((key) => {
formData.append(myFiles.item(key).name, myFiles.item(key));
});
const response = await fetch('http://localhost:3000/api/news', {
method: 'POST',
body: formData,
});
const json = await response.json();
const h2 = document.querySelector('h2');
h2.textContent = `Status: ${json?.status}`;
const h3 = document.querySelector('h3');
h3.textContent = json?.message;
console.log(json);
};
form.addEventListener('submit', (e) => {
e.preventDefault();
sendFiles();
});
</script>
</body>
</html>