-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
172 lines (165 loc) · 5.97 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
<!DOCTYPE html>
<html lang="en">
<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">
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="http://cdn.bootcss.com/blueimp-md5/1.1.0/js/md5.min.js"></script>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css">
<title>HTML5 文件API</title>
<style>
span{
display: block;
}
.progress-outer{
width: 150px;
height: 25px;
background: #ccc;
border-radius: 5px;
}
.file-progress{
height: 100%;
background: #28a051;
color: white;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="row">
<div class="col-md-6">
<form class="form-inline" role="form">
<div class="form-group">
<label class="form-label">选择文件: </label>
<input type="file" class="form-control" id="fileinput">
</div>
<div class="form-group">
<a id="submit" data-target="#progress-modal" data-toggle="modal" class="btn btn-xs btn-primary">SUBMIT</a>
</div>
</form>
</div>
</div>
<div class="modal fade" id="progress-modal">
<div class="modal-dialog" style="width: 1200px;">
<div class="modal-content">
<div class="modal-header">
上传进度
</div>
<div class="modal-body">
<table class="table table-striped">
<thead>
<tr>
<th>文件名</th>
<th>文件大小</th>
<th>更新时间</th>
<th>上传进度</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<tr></tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
<script>
$("body").on("click", "#submit", function(){
var files = document.querySelector("#fileinput").files;
console.log(files)
if(!files.length){
alert("当前没有选择文件");
return false;
}
/**
* 获取基本信息
*/
var fileInfoStr = '<td>' + files[0].name + '</td>' +
'<td>' + files[0].size + '</td>' +
'<td>' + files[0].lastModifiedDate + '</td>' +
'<td><span id="progress-outer"><span class="file-progress"></span></span></td>' +
'<td><button id="restart" class="btn btn-xs btn-primary">重新上传</button> ' +
'<button id="stop" class="btn btn-xs btn-primary">停止上传</button> ' +
'<button id="continue" class="btn btn-xs btn-primary">继续上传</button></td>';
$(".table tbody tr").html(fileInfoStr);
/**
* 开始上传
*/
var md5info = md5(files[0].name);
//相关变量
var startPos = parseInt(window.localStorage.getItem(md5info)) || 0,
file = files[0],
stepSize = 1 * 1024 * 1024,
totalStep = Math.ceil(file.size / stepSize),
fileSize = file.size;
if(startPos === totalStep - 1){
alert("当前文件已上传,请不要重复上传");
return false;
}
var reader = new FileReader();
readBlob();
reader.onload = function(e){
loadedCb(e);
}
//停止
$("body").on("click", "#stop", function(){
reader = null;
return false;
}).on("click", "#continue", function(){
reader = new FileReader();
reader.onload = function(e){
loadedCb(e);
}
startPos = parseInt(window.localStorage.getItem(md5info));
readBlob();
}).on("click", "#restart", function(){
if(!reader){
reader = new FileReader();
reader.onload = function(e){
loadedCb(e);
}
}
startPos = 0;
readBlob();
})
//分段读取函数
function readBlob(){
var blob;
if(file.webkitSlice){
blob = file.webkitSlice(stepSize * startPos, stepSize * (startPos + 1));
}else if(file.mozSlice){
blob = file.mozSlice(stepSize * startPos, stepSize * (startPos + 1));
}else if(file.slice){
blob = file.slice(stepSize * startPos, stepSize * (startPos + 1));
}else{
alert("浏览器不支持分段读取");
return false;
}
reader && reader.readAsArrayBuffer(blob);
}
//读取完成回调
function loadedCb(e){
//计算进度
var fileLoaded = startPos * stepSize + e.total,
percent = Math.floor(fileLoaded / fileSize * 100) + '%';
$(".file-progress").text(percent).css('width', percent);
//保存进度
window.localStorage.setItem(md5info, startPos);
//是否继续
if(startPos < totalStep - 1){
startPos++
setTimeout(readBlob, 2000)
//readBlob();
}else{
$(".file-progress").text('100%');
window.localStorage.removeItem(md5info);
alert("上传成功啦")
}
}
})
</script>
</body>
</html>