-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
179 lines (163 loc) · 5.7 KB
/
Copy pathindex.html
File metadata and controls
179 lines (163 loc) · 5.7 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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>File API 客製上傳檔案按鈕(input file) - August - Let's Write</title>
<meta property="fb:app_id" content="484081328635964"/>
<meta property="og:url" content="https://letswritetw.github.io/letswrite-file-api-custom-input/"/>
<meta property="og:type" content="website"/>
<meta property="og:site_name" content="Let's Write"/>
<meta property="og:title" content="File API 客製上傳檔案按鈕(input file) - Let's Write"/>
<meta property="og:description" content="File API 客製上傳檔案按鈕(input file) - Let's Write"/>
<meta property="og:image" content="https://letswritetw.github.io/letswrite-file-api-custom-input/fb.jpg"/>
<meta property="og:image:width" content="1200"/>
<meta property="og:image:height" content="630"/>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/normalize/8.0.1/normalize.min.css">
<style>
*, *::before, *::after {
box-sizing: border-box;
}
html, body, .container {
width: 100%;
height: 100%;
overflow: hidden;
}
body {
background: #F5F5F5;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol", "微軟正黑體", "Microsoft JhengHei";
color: #555;
}
.container {
display: flex;
justify-content: center;
align-content: center;
flex-wrap: wrap;
margin-right: auto;
margin-left: auto;
max-width: 480px;
}
h1, h6 {
margin: 0;
}
h1 {
margin-bottom: 16px;
font-size: 28px;
}
h6 {
margin-bottom: 50px;
font-weight: normal;
font-size: 14px;
}
h6 a {
text-decoration: none;
color: #00A7E5;
}
h6 a:hover {
color: #008BBF;
}
input {
display: none;
}
label {
display: flex;
justify-content: center;
align-items: center;
margin-bottom: 32px;
padding: 8px 32px;
background: #FFF;
border: 1px solid #CCC;
border-radius: 50px;
cursor: pointer;
transition: box-shadow .4s ease;
}
label:hover {
box-shadow: 0 10px 20px rgba(0, 0, 0, .19), 0 6px 6px rgba(0, 0, 0, .23);
}
label span {
margin-left: 12px;
font-size: 30px;
}
.icon {
width: 4vw;
}
figure {
display: block;
margin: 0;
}
#file_thumbnail {
display: block;
max-width: 100%;
border: 1px solid #CCC;
}
.info-box {
width: 100%;
}
@media screen and (max-width: 480px) {
.container {
padding-right: 12px;
padding-left: 12px;
}
label span {
font-size: 20px;
}
}
</style>
<link rel="shortcut icon" href="https://letswritetw.github.io/letswritetw/dist/img/logo_512.png"/>
<!-- Google Tag Manager-->
<script>
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode.insertBefore(j,f);
})(window,document,'script','dataLayer','GTM-PGQ9WQT');
</script>
</head>
<body>
<!-- Google Tag Manager (noscript)-->
<noscript>
<iframe src="https://www.googletagmanager.com/ns.html?id=GTM-PGQ9WQT" height="0" width="0" style="display:none;visibility:hidden"></iframe>
</noscript>
<div class="container">
<h1>File API 客製上傳檔案按鈕(input file)</h1>
<h6>
說明文章:
<a href="https://letswrite.tw/file-api-custom-input/" target="_blank">File API 客製上傳檔案按鈕(input file) - Let's Write</a>
</h6>
<section class="button-box">
<input id="customFileInput" type="file" accept="image/*">
<label for="customFileInput" class="button-primary">
<img class="icon" src="upload.svg" alt="上傳檔案">
<span>客製選擇檔案按鈕</span>
</label>
</section>
<section class="info-box">
<p>檔名:<span id="file_name"></span></p>
<p>類型:<span id="file_type"></span></p>
<p>大小:<span id="file_size"></span></p>
<p>最後更新時間:<span id="file_time"></span></p>
<p>縮圖:</p>
<figure>
<img id="file_thumbnail">
</figure>
</section>
</div>
<script>
var inputFile = document.getElementById('customFileInput');
inputFile.addEventListener('change', function(e) {
var fileData = e.target.files[0]; // 檔案資訊
var fileName = fileData.name; // 檔案名稱
var fileType = fileData.type; // 檔案類型
var fileSize = Math.floor(fileData.size * 0.001); // 檔案大小轉成kb
var fileTime = fileData.lastModifiedDate;
console.log(fileData); // 用開發人員工具可看到資料
document.getElementById('file_name').innerText = fileName;
document.getElementById('file_type').innerText = fileType;
document.getElementById('file_size').innerText = fileSize + 'kb';
document.getElementById('file_time').innerText = fileTime;
document.getElementById('file_thumbnail').src = URL.createObjectURL(fileData);
}, false);
</script>
</body>
</html>