-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathindex.html
337 lines (293 loc) · 11.6 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
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn" dir="ltr">
<head>
<meta charset="utf-8" />
<title>七牛云存储 | HTML5 大文件上传</title>
<link href="http://netdna.bootstrapcdn.com/bootstrap/3.0.0/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
thead {
background-color: #efefef;
}
.wrap {
width:800px;
margin: 0 auto;
margin-top: 50px;
margin-bottom: 100px;
}
.fileupload {
/*width: 300px;*/
margin: 0 auto;
}
#progressbar {
float: left;
width:350px;
height: 20px;
}
.progress {
/*width:300px;*/
margin-bottom: 0px;
}
.toolBar {
width:500px;
margin: 0 auto;
margin-top: 50px;
}
.bar {
text-align:left;
}
header {
text-align: center;
}
li {
list-style-type: none;
}
.td_fname {
width: 250px;
height: 70px;
}
td {
vertical-align:middle;
border: 1px solid #ccc;
padding: 5px;
}
.pLabel {
position: absolute;
width: 300px;
text-align: center;
color: black;
}
.alert {
position: fixed;
bottom: 0px;
}
* {
margin:0;
padding:0;
}
a {
text-decoration:none;
}
.btn_addPic {
display: block;
position: relative;
cursor: pointer;
text-align: center;
}
.btn_addPic span {
display: block;
}
.btn_addPic em {
display: inline-block;
width: 18px;
height: 18px;
overflow: hidden;
margin: 10px 5px 10px 0;
line-height: 20em;
vertical-align: middle;
}
.filePrew {
display: block;
position: absolute;
top: 0;
left: 0;
width: 400px;
height: 40px;
font-size: 100px;
/* 增大不同浏览器的可点击区域 */
opacity: 0;
/* 实现的关键点 */
}
.td_pro{
width: 400px;
}
footer{
text-align:center;
}
</style>
</head>
<body>
<header>
<h3>七牛云存储</h3>
<hr>
</header>
<section>
<div class="fileupload fileupload-new" style="width:300px" data-provides="fileupload">
<a href="javascript:void(0);" class="btn_addPic btn btn-default btn-lg">
<span>
<i class="glyphicon glyphicon-plus"></i>
添加文件(可同时选择多个文件)
</span>
<input id="selectFiles" multiple="multiple" size="3" name="pic" class="filePrew" type="file">
</a>
</div>
<div class="wrap">
<div id="spdiv" class="input-group" style="display:none;">
<span class="input-group-addon">
<span class="glyphicon glyphicon-home"></span>
</span>
<input id="txt_serverPath" type="text" class="form-control" placeholder="请输入服务端目录">
</div>
<div id="fileList">
</div>
</div>
</section>
<footer>
<a href="https://github.com/icattlecoder/7niu" target="blank" style="width:260px" class="btn btn-default btn-lg">
<img src="http://qtestbucket.qiniudn.com/GitHub-Mark-32px.png"> View Source On GitHub</a>
</footer>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<!-- * -->
<script type="text/javascript" src="static/js/bootbox.js"></script>
<!-- * -->
<script type="text/javascript" src="crc32.js?id=12"></script>
<script type="text/javascript" src="qiniu.uploader.js?id=12"></script>
<script type="text/javascript" src="static/js/bootstrap-fileupload.min.js"></script>
<script src="http://cdnjs.bootcss.com/ajax/libs/twitter-bootstrap/2.3.1/js/bootstrap.min.js"></script>
<script src="static/js/bootstrap.js"></script>
<script type="text/javascript">
//“显示文件信息”按钮的click事件代码
$(function() {
var Bucket = "qtestbucket";
Q.Histroy(false);
Q.SignUrl("index.php");
Q.Bucket(Bucket);
$("#selectFiles").change(function() {
$("#btn_upload").prop("disabled", false)
var files_arr = [];
Q.Stop();
var idx = 0;
files = document.getElementById("selectFiles").files;
if (files && files.length) {
$("#spdiv").show();
var pro = function(i) {
return '<div class="progress"> <div id="progressbar' + i + '" class="progress-bar" role="progressbar" aria-valuenow="60" aria-valuemin="0" aria-valuemax="100" style="width: 0%;"> </div><div class="pLabel" id="progressbarLabel' + i + '"></div> </div>';
}
var tmp = '<table style="width:800px;margin-top:20px;">';
tmp += "<thead><tr><td>文件名称</td><td>文件大小</td><td>状态</td></tr></thead><tbody>"
for (i = 0; i < files.length; i++) {
files_arr.push(files[files.length - i - 1]);
tmp += "<tr>";
tmp += '<td class="td_fname">' + files[i].name + "</td><td>" + Q.fileSize(files[i].size) + "</td>";
tmp += '<td class="td_pro" id="td_' + i + '">' + pro(i) + '</td>';
tmp += "</tr>"
}
tmp += '<td colspan="4" style="text-align:center;"><button id="btn_upload" state="start" class="btn_upload btn btn-default btn-large btn-lg" ><span class="glyphicon glyphicon-cloud-upload"></span> 上 传 </button></td>'
tmp += "</tbody></table>"
$("#fileList").html(tmp);
$("#btn_upload").on("click", function() {
if (Q.IsUploading()) {
//alert("正在上传")
//return
}
if ($(this).attr("state") == "pause") {
$(this).attr("state", "go-on");
$(this).html('<span class="glyphicon glyphicon-play"></span>继续');
Q.Pause();
return;
}
if ($(this).attr("state") == "go-on") {
$(this).attr("state", "pause");
$(this).html('<span class="glyphicon glyphicon-pause"></span>暂停');
Q.Resume();
return;
}
var getKey = function(subkey) {
var d = $("#txt_serverPath").val();
if (d == "") {
return subkey;
}
var s = 0;
while (d[s] == "/") {
s++;
}
var e = d.length - 1;
while (d[e] == "/") {
e--;
}
d = d.substr(s, e - s + 1);
return d + "/" + subkey;
}
if ($(this).attr("state") == "start") {
// Q.SignUrl("http://42.62.26.6/qiniu/index.php");
Q.addEvent("historyFound", function(his) {
if (confirm("文件:" + his + ",未上传完成,是否继续?")) {
Q.ResumeHistory();
} else {
Q.ClearHistory();
// var key = dir +"/"+Q.getFile().name;
// Q.Upload(Q.getFile().name);
}
});
//可以在此回调中添加提交至服务端的额外参数,用于生成上传token
//putExtra会
Q.addEvent("beforeUp", function() {
extra = new Object();
var key = getKey(Q.getFile().name);
extra.key = key;
console.log(JSON.stringify(extra));
Q.SetPutExtra(JSON.stringify(extra));
});
//上传失败回调
Q.addEvent("putFailure", function(msg) {
$("#td_" + idx).html(
'<strong>上传失败: </strong> ' + msg
)
idx++;
up();
});
//上传进度回调
// p:0~100
Q.addEvent("progress", function(p, s) {
$("#progressbar" + idx).attr("style", "width: " + p + "%")
$("#progressbarLabel" + idx).text(p + "%" + ", 速度: " + s);
});
//上传完成回调
//fsize:文件大小(MB)
//res:上传返回结果,默认为{hash:<hash>,key:<key>}
Q.addEvent("putFinished", function(fsize, res, taking) {
uploadSpeed = 1024 * fsize / (taking * 1000);
if (uploadSpeed > 1024) {
formatSpeed = (uploadSpeed / 1024).toFixed(2) + "Mb\/s";
} else {
formatSpeed = uploadSpeed.toFixed(2) + "Kb\/s";
};
$("#btn_upload").attr("state", "start");
$("#btn_upload").html('<span class="glyphicon glyphicon-cloud-upload"></span>上传');
$("#td_" + idx).html(
'<strong>文件地址: </strong> <a href="http://' + Bucket + '.qiniudn.com/' + res.key + '" target="_blank" >http://' + Bucket + '.qiniudn.com/' + res.key + '</a><br/>' +
'<strong>Hash: </strong>' + res.hash
)
$("#progressbar" + idx).attr("style", "width: 100%")
$("#progressbarLabel" + idx).html("上传成功!平均速度:" + formatSpeed);
idx++;
up();
});
var up = function() {
console.log("up...")
if (files_arr && files_arr.length) {
$("#btn_upload").prop("disabled", true)
var f = files_arr.pop();
var key = getKey(f.name);
$(this).attr("state", "pause");
$(this).html('<span class="glyphicon glyphicon-pause"></span>暂停');
console.log("uploading file:", f.name, " key:", key);
Q.Upload(f, key);
} else {
$("#btn_upload").prop("disabled", false)
bootbox.alert("所有文件上传完成!");
}
}
up();
return;
}
})
} else {
$("#spdiv").hidden();
}
});
$("#upladBtn").click(function(event) {
});
});
</script>
</body>
</html>