-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·232 lines (222 loc) · 8.67 KB
/
index.js
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
//JavaScript上传核心
var dom=document.getElementsByTagName('form')[0];
var xhr=new XMLHttpRequest();
var fd;//文件分割片段
var file;//文件指针
const LENGTH=1*1024*1024;//分片长度
var start;//分片起始位置
var end;//分片结束位置
var filename;//文件名
var hash;//文件哈希值
var pkgnum=0;//当前文件分片位置
function upfile(){//上传文件准备
/* document.getElementById("progress").style.display="block";
document.getElementById("upimg").style.display="block";//显示提示框
document.getElementById("load").style.display="block";*/
$(function () {
$("#upinfobu").popover('show');
});
start=0;//开始节点
end=LENGTH+start;//终止节点
file=document.getElementById('filei').files[0];
filename=file.name.replace(/\s/g,"_")//反空格技术
if(!file){
alert('请选择文件');
return;
}
document.getElementById("load").innerHTML="计算哈希值……";
document.getElementById("infos").innerHTML="计算哈希值……";
console.log("计算哈希值……");
let reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function () {//计算sha256
var wordArray = CryptoJS.lib.WordArray.create(reader.result);
hash = CryptoJS.SHA256(wordArray).toString();
document.getElementById("load").innerHTML="";
var xhrboth=new XMLHttpRequest();//检查文件是否重复
xhrboth.open("POST","api/check.php",true);
var dot=new FormData;
dot.append("hash",hash);
dot.append("filename",filename);
xhrboth.send(dot);
xhrboth.onreadystatechange=function(){
if(this.readyState==4){
if(this.status==200 && this.responseText=="notboth"){//无重复
up();
return;
}else if(this.status==200 && this.responseText.split('!')[0]=="haveundone"){//发现应断点续传
pkgnum=this.responseText.split('!')[1];//同步分片位置
pkgnum=parseInt(pkgnum);
pkgnum+=1;
start=LENGTH*pkgnum;
end=start+LENGTH;
up();
}else if(this.status==401){
$(function () {
$("#upinfobu").popover('hide');
});
alert("未登录")
auth();
}
else{
erroring(xhrboth.responseText);
/*document.getElementById("progress").style.display="none";
document.getElementById("upimg").style.display="none";
document.getElementById("load").style.display="none";*/
$(function () {
$("#upinfobu").popover('hide');
});
return;
}
}
}
};
}
function up(){//上传文件
if(start<file.size){
xhr.open('POST','api/file.php',true);//传文件
xhr.onreadystatechange=function(){
if(this.readyState==4){
if(this.status>=200&&this.status<300){
if(this.responseText!="done"){//失败则报告并停止上传
alert(this.responseText);
document.getElementById('upimg').style.width='0%';
/*document.getElementById("progress").style.display="none";
document.getElementById("upimg").style.display="none";
document.getElementById("load").style.display="none";*/
$(function () {
$("#upinfobu").popover('hide');
});
}else{
start=end;
end=start+LENGTH;
up();//自调用来接着传
}
}else if(this.status==401){
$(function () {
$("#upinfobu").popover('hide');
});
alert("未登录");
auth();
}else{
alert("网络错误"+this.status)
}
}
}
xhr.upload.onprogress=function(ev){
if(ev.lengthComputable){
var pecent=100*(ev.loaded+start)/file.size;
if(pecent>100){
pecent=100;
}
//num.innerHTML=parseInt(pecent)+'%';
document.getElementById('upimg').style.width=pecent+'%';
document.getElementById('load').innerHTML = parseInt(pecent)+'%'
document.getElementById('infos').innerHTML ="已上传:"+ parseInt(pecent)+'%'
}
}
//分割文件核心部分slice
var blob=file.slice(start,end);
fd=new FormData();
fd.append('mof',blob);//文件
fd.append('filename',filename);//文件名
fd.append('pkgnum',pkgnum)//当前包数
fd.append('hash',hash)
//console.log(fd);
console.log("正在上传包"+pkgnum)
xhr.send(fd);
pkgnum=parseInt(pkgnum)+1;
}else{//传完了提醒发下sql
xhrend=new XMLHttpRequest();
end=new FormData();
xhrend.open("POST","api/ok.php",true);
end.append("size",file.size);
end.append("filename",filename);
end.append("hash",hash);
xhrend.send(end);
xhrend.onreadystatechange=function(){
if(this.readyState==4){
if(this.status==200 && this.responseText=="done"){
/*document.getElementById("progress").style.display="none";
document.getElementById("upimg").style.display="none";
document.getElementById("load").style.display="none";*/
$(function () {
$("#upinfobu").popover('hide');
});
document.location = 'index.php?success=上传成功'
return;
}else if(this.status==401){
$(function () {
$("#upinfobu").popover('hide');
});
alert("未登录")
auth();
}
else{
alert(this.responseText);
/*document.getElementById("progress").style.display="none";
document.getElementById("upimg").style.display="none";
document.getElementById("load").style.display="none";*/
$(function () {
$("#upinfobu").popover('hide');
});
return;
}
}
}
return;
}
}
function successing(what){
document.getElementById("warn").style.animation="warn 4s forwards";
document.getElementById("warn").innerHTML='<div class="alert alert-success alert-dismissable fade in">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">'+
'×'+
'</button>'+
what
+'</div>';
setTimeout(function() {
document.getElementById("warn").innerHTML="";
document.getElementById("warn").style.animation="";
}, 4001);
}
function erroring(what){
document.getElementById("warn").style.animation="warn 4s forwards";
document.getElementById("warn").innerHTML='<div class="alert alert-danger alert-dismissable fade in">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">'+
'×'+
'</button>'+
what
+'</div>';
setTimeout(function() {
document.getElementById("warn").style.animation="";
document.getElementById("warn").innerHTML="";
}, 4001);
}
function infoing(what){
document.getElementById("warn").style.animation="warn 4s forwards";
document.getElementById("warn").innerHTML='<div class="alert alert-info alert-dismissable fade in">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">'+
'×'+
'</button>'+
what
+'</div>';
setTimeout(function() {
document.getElementById("warn").style.animation="";
document.getElementById("warn").innerHTML="";
}, 4001);
}
function warning(what){
document.getElementById("warn").style.animation="warn 4s forwards";
document.getElementById("warn").innerHTML='<div class="alert alert-warning alert-dismissable fade in">'+
'<button type="button" class="close" data-dismiss="alert" aria-hidden="true">'+
'×'+
'</button>'+
what
+'</div>';
setTimeout(function() {
document.getElementById("warn").style.animation="";
document.getElementById("warn").innerHTML="";
}, 4001);
}