-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshare.html
306 lines (285 loc) · 9.31 KB
/
share.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
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
<!DOCTYPE html>
<html lang="zh-CN">
<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>用剪切板和BASE64传输文件</title>
<style>
* {
padding: 0;
margin: 0;
}
.container {
width: 400px;
margin: 0 auto;
}
.nav {
display: flex;
justify-content: space-around;
border-bottom: 1px solid #dcdfe6;
}
.nav-item {
color: #1989fa;
opacity: 0.5;
padding: 15px 10px;
cursor: pointer;
user-select: none;
}
.active {
opacity: 1;
border-bottom: #1989fa 3px solid;
font-weight: bold;
}
.upload-file-button-group {
display: flex;
justify-content: space-between;
margin: 20px 0;
}
.action-button {
display: inline-block;
background-color: #409eff;
color: white;
padding: 7px 16px;
border-radius: 20px;
cursor: pointer;
/* margin-right: 20px; */
user-select: none;
}
.copy-button {
background-color: rgb(236, 245, 255);
/* opacity: 0.6; */
color: #409eff;
border: #409eff 1px solid;
}
#base64-result {
border: 3px solid rgb(192, 189, 189);
height: 200px;
word-wrap: break-word;
overflow: auto;
}
.placeholder-text {
color: rgb(192, 189, 189);
margin-top: 20px;
}
.filename-input,
#download-file-base64 {
outline-style: none;
border: 1px solid #ccc;
border-radius: 3px;
padding: 6px;
font-size: 14px;
width: 100%;
margin: 10px 0;
box-sizing: border-box;
}
.filename-input:focus,
#download-file-base64:focus {
border-color: #66afe9;
outline: 0;
-webkit-box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
0 0 8px rgba(102, 175, 233, 0.6);
box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075),
0 0 8px rgba(102, 175, 233, 0.6);
}
.download-button-area {
display: flex;
justify-content: center;
}
#download-button {
width: 120px;
display: flex;
justify-content: center;
}
</style>
</head>
<body>
<div class="container">
<div class="nav">
<div
id="show-upload-file-page-button"
class="nav-item active"
onclick="showUploadFile()"
>
上传文件
</div>
<div
id="show-download-file-page-button"
class="nav-item"
onclick="showDownloadFile()"
>
下载文件
</div>
</div>
<!-- <hr /> -->
<div class="operation-area">
<div id="file-to-base64">
<div class="upload-file-button-group">
<div
class="action-button choose-file-button"
onclick="handleUploadFileButtonClick()"
>
选择文件
</div>
<div onclick="copyResult()" class="action-button copy-button">
复制结果
</div>
</div>
<input
type="file"
name=""
id="upload-file-input"
style="display: none"
/>
<!-- <h3>上传结果</h3> -->
<div id="base64-result">
<span class="placeholder-text">上传结果</span>
</div>
<div class="usage">
使用说明:
<p></p>
理论上来说本工具的文件大小限制就是系统、浏览器、剪切板的长度限制,但是实际使用中文件过大很容易造成网页应用崩溃,经过多次实践发现3M左右是比较合适的单文件大小,如果有更大的文件可以分卷压缩后传输
</div>
</div>
<div id="base64-to-file" style="display: none">
<input
class="filename-input"
type="text"
id="file-name"
placeholder="请输入文件名"
/>
<textarea
name=""
id="download-file-base64"
cols="30"
rows="10"
placeholder="请将文件base64编码粘贴在这里"
></textarea>
<div class="download-button-area">
<div
onclick="downloadFileByBase64()"
id="download-button"
class="action-button"
>
下载
</div>
</div>
<div class="usage">
使用说明:
<p></p>
1.如果输入完整的文件名,下载的文件就以输入的文件名为准
<p></p>
2.如果只输入了文件名没有文件类型后缀,会根据BASE64中的文件类型信息尽可能自动添加后缀
<p></p>
3.如果不输入文件名,文件名将会是
"下载文件"+当前时间戳,文件后缀会根据BASE64中的文件类型信息尽可能自动添加后缀
</div>
</div>
</div>
</div>
<script>
var uploadResult = "";
function showUploadFile() {
document.getElementById("file-to-base64").style.display = "block";
document.getElementById("base64-to-file").style.display = "none";
document
.getElementById("show-upload-file-page-button")
.classList.add("active");
document
.getElementById("show-download-file-page-button")
.classList.remove("active");
}
function showDownloadFile() {
document.getElementById("file-to-base64").style.display = "none";
document.getElementById("base64-to-file").style.display = "block";
document
.getElementById("show-upload-file-page-button")
.classList.remove("active");
document
.getElementById("show-download-file-page-button")
.classList.add("active");
}
function handleUploadFileButtonClick() {
document.getElementById("upload-file-input").click();
}
document.getElementById("upload-file-input").onchange = function () {
var fileReader = new FileReader();
fileReader.readAsDataURL(this.files[0]);
fileReader.onload = function () {
uploadResult = fileReader.result;
document.getElementById("base64-result").innerHTML =
fileReader.result;
copyResult();
};
};
function dataURLtoFile(dataurl, filename) {
//将base64转换为文件
var arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new File([u8arr], filename, { type: mime });
}
function dataURLtoBlob(dataurl) {
var arr = dataurl.split(","),
mime = arr[0].match(/:(.*?);/)[1],
bstr = atob(arr[1]),
n = bstr.length,
u8arr = new Uint8Array(n);
while (n--) {
u8arr[n] = bstr.charCodeAt(n);
}
return new Blob([u8arr], { type: mime });
}
function downloadFile(url, name) {
var a = document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("download", name);
a.setAttribute("target", "_blank");
let clickEvent = document.createEvent("MouseEvents");
clickEvent.initEvent("click", true, true);
a.dispatchEvent(clickEvent);
}
function downloadFileByBase64() {
let base64 = document.getElementById("download-file-base64").value;
if (base64.trim() === "") return alert("请先粘贴文件的BASE64编码");
let fileName = document.getElementById("file-name").value;
if (fileName.trim() == "") {
fileName = "下载文件" + new Date().valueOf();
}
var myBlob = dataURLtoBlob(base64);
var myUrl = URL.createObjectURL(myBlob);
downloadFile(myUrl, fileName);
}
function copyResult() {
if (uploadResult.trim() === "") {
alert("请先上传文件");
} else {
if (navigator.clipboard) {
// clipboard api 复制
navigator.clipboard.writeText(uploadResult);
} else {
var textarea = document.createElement("textarea");
document.body.appendChild(textarea);
// 隐藏此输入框
textarea.style.position = "fixed";
textarea.style.clip = "rect(0 0 0 0)";
textarea.style.top = "10px";
// 赋值
textarea.value = uploadResult;
// 选中
textarea.select();
// 复制
document.execCommand("copy", true);
// 移除输入框
document.body.removeChild(textarea);
}
}
}
</script>
</body>
</html>