-
Notifications
You must be signed in to change notification settings - Fork 27.1k
/
Copy pathstyled-file-picker.html
108 lines (92 loc) · 2.65 KB
/
styled-file-picker.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Styled file picker example</title>
<link href="https://fonts.googleapis.com/css?family=Josefin+Sans&display=swap" rel="stylesheet">
<style>
body {
font-family: 'Josefin Sans', sans-serif;
margin: 20px auto;
max-width: 400px;
}
form > div {
margin-bottom: 20px;
}
button, label, input {
display: block;
font-family: inherit;
font-size: 100%;
padding: 0;
margin: 0;
box-sizing: border-box;
width: 100%;
padding: 5px;
height: 30px;
}
input[type="file"] {
height: 0;
padding: 0;
opacity: 0;
}
label[for="file"] {
box-shadow: 1px 1px 3px #ccc;
background: linear-gradient(to bottom, #eee, #ccc);
border: 1px solid rgb(169, 169, 169);
border-radius: 5px;
text-align: center;
line-height: 1.5;
}
label[for="file"]:hover {
background: linear-gradient(to bottom, #fff, #ddd);
}
label[for="file"]:active {
box-shadow: inset 1px 1px 3px #ccc;
}
button {
width: 60%;
margin: 0 auto;
}
</style>
</head>
<body>
<form>
<div>
<label for="file">Choose a file to upload</label>
<input id="file" name="file" type="file" multiple>
<ul id="file-list">
</ul>
</div>
<div><button>Submit?</button></div>
</form>
<script>
const fileInput = document.querySelector('#file');
const fileList = document.querySelector('#file-list');
fileInput.addEventListener('change', updateFileList);
function updateFileList() {
while(fileList.firstChild) {
fileList.removeChild(fileList.firstChild);
}
let curFiles = fileInput.files;
if(!(curFiles.length === 0)) {
console.log('test');
for(let i = 0; i < curFiles.length; i++) {
const listItem = document.createElement('li');
listItem.textContent = 'File name: ' + curFiles[i].name + '; file size ' + returnFileSize(curFiles[i].size) + '.';
fileList.appendChild(listItem);
}
}
}
function returnFileSize(number) {
if(number < 1024) {
return number + 'bytes';
} else if(number >= 1024 && number < 1048576) {
return (number/1024).toFixed(1) + 'KB';
} else if(number >= 1048576) {
return (number/1048576).toFixed(1) + 'MB';
}
}
</script>
</body>
</html>