better-staridc-MNBT
1/*
2 * 梦奈宝塔主机系统(简称MNBT)
3 * 宝塔文件分片上传前端js
4*/
5function pro(bolb,sizes,path){ //上传文件,此分片文件,上次返回的大小,路径
6var formdata = new FormData();
7formdata.append("file",bolb);
8formdata.append("tempfilename",fileuploadname);
9formdata.append("gn","fileupload");
10formdata.append("fesw",sizes);
11formdata.append("zsize",file.size);
12formdata.append("htl",path);
13let data;
14 $.ajax({
15 type:"post",
16 url:"./ajax.php",
17 data:formdata,
18 cache: false,
19 processData: false,
20 contentType: false,
21 async:true,
22 success:function(res){
23 uploadsfdyc++;
24 try{
25 var json= JSON.parse(res);
26 }catch(e){
27 msalert(4,'一个意外的错误终止的本次上传!');
28
29 $.confirm({
30 title: '一个意外的错误',
31 content: res,
32 icon: 'mdi mdi-close-circle',
33 animation: 'scale',
34 closeAnimation: 'scale',
35 opacity: 0.5,
36 type:'red',
37 buttons: {
38 'confirm': {
39 text: '我知道了',
40 btnClass: 'btn-blue'
41 }
42 }
43 });
44 msloadingde(); // 隐藏
45 return;
46 }
47 if(json.error==1){
48 //上传完成结束
49 msalert(json.size,json.msg,4000);
50 $("#tb_departments").bootstrapTable('refreshOptions',{pageNumber:1}); //刷新表格
51 msloadingde();
52 }else{
53 //开始下一片上传
54 if(json.size>=file.size){
55 $(".custom-file-label").html('选择文件...');
56 msalert(4,'出现意外的错误!请删除名称为'+fileuploadname+'.'+file.size+'.upload.tmp的文件!然后重新上传!', 6000);
57 msloadingde(); // 隐藏
58 }else{
59 var filebolb=filefp(json.size);
60 pro(filebolb,json.size,path);
61 }
62 }
63 }
64 });
65}
66
67function filefp(filesizey){ //文件分片(已上传大小)
68 fileupload(filesizey,lengths); //更新上传提示
69 if(uploadsfdyc<=2){
70 lengths=lengths; //下次分片上传的文件大小
71 }else{
72 lengths=(upxcsize.toFixed(0))*2; //下次分片上传的文件大小
73 }
74 var end=filesizey+lengths; //获取文件下次结尾大小
75 if(end>file.size){
76 end=file.size; //如果结尾大于文件大小那么下次结尾就是文件大小。
77 }
78 var bolb = file.slice(filesizey,end); //文件分片
79 return bolb;
80}
81
82function zxwjsc(hliy){ //文件上传
83var myfile = document.getElementById("myfile");
84uploadsfdyc=0; //已完成上传几次分片
85file = myfile.files[0];
86if(file==null){msalert(3,'请选择要上传的文件!',4000,'#exampleModal');return;}
87//判断是否存在
88 var selRows = $("#tb_departments").bootstrapTable("getData",{useCurrentPage:true});
89 var arrs = '';
90 $.each(selRows,function(i) {
91 if(this.name==file.name){
92 if(this.type=='file'){
93 arrs=this;
94 }
95 }
96 });
97
98$('#exampleModal').modal('hide'); //关闭弹窗
99if(arrs!=''){
100 $.confirm({
101 title: '即将覆盖以下文件',
102 content: '该目录中存在着与即将上传文件相同名称的文件!请选择操作!<br/><span class="input-group-text">操作类型:<div class="custom-control custom-radio custom-control-inline"><input type="radio" id="customRadioInline1" name="customRadioInline" class="custom-control-input" onclick="$(`#input-namesk`).val(file.name); $(`#input-namesk`).attr(`readonly`,`readonly`);"><label class="custom-control-label" for="customRadioInline1">覆盖文件</label></div><div class="custom-control custom-radio custom-control-inline"><input type="radio" id="customRadioInline2" name="customRadioInline" class="custom-control-input" onclick="$(`#input-namesk`).val(file.name+`-副本`); $(`#input-namesk`).removeAttr(`readonly`);" checked><label class="custom-control-label" for="customRadioInline2">重命名文件</label></div></span><div class="input-group"><div class="input-group-prepend"><span class="input-group-text" id="inputGroupPrepend2">文件名:</span></div><input type="text" class="form-control border" id="input-namesk" aria-describedby="inputGroupPrepend2" placeholder="文件名" value="'+file.name+'-副本" required></div><span class="input-group-text">大小:'+sizedwhs(arrs.size)+'</span><span class="input-group-text">最后修改时间:'+getTimes(arrs.mtime)+'</span>',
103 icon: 'mdi mdi-comment-question',
104 animation: 'scale',
105 closeAnimation: 'scale',
106 opacity: 0.5,
107 type:'orange',
108 buttons: {
109 'confirm': {
110 text: '确认',
111 btnClass: 'btn-blue',
112 action: function(){
113 var input = this.$content.find('input#input-namesk').val();
114 fileuploadname=input;
115 uploadcode(hliy);
116
117 }
118 },
119 '取消': function(){}
120 }
121 });
122}else{
123fileuploadname=file.name;
124uploadcode(hliy);
125}
126}
127
128function uploadcode(hliy){ //断点续传验证
129lengths = 1024 * 1024 * 1; //默认每片1MB大小
130msloading('文件上传中,共'+sizedwhs(file.size)+',已上传0.00MB,剩余'+sizedwhs(file.size)+'未上传,当前速度0.00MB/s,预计剩余时间获取中,文件上传进度0.00%');
131
132let data = {};
133data["gn"]="file_upload_size";
134data["fename"]=fileuploadname;
135data["htl"]=hliy;
136data["size"]=file.size;
137$.post('./ajax.php', data, function (date) {
138 try {
139 var jsoe= JSON.parse(date);
140 } catch (e) {
141 msloadingde();
142 msalert(4,'系统错误,无法上传');
143 return;
144 }
145
146if(jsoe.code!==1 || jsoe.size===undefined){
147 msloadingde();
148 msalert(4,jsoe.code);
149 return;
150}
151
152ot = new Date().getTime(); //设置上传开始时间
153var bolb=filefp(jsoe.size);
154pro(bolb,jsoe.size,hliy);
155})
156}
157
158function sizedwhs(size){ //文件单位换算
159 var units = 'B';
160 if(size/1024>1){
161 size = size/1024;
162 units = 'KB';
163 }
164 if(size/1024>1){
165 size = size/1024;
166 units = 'MB';
167 }
168 if(size/1024>1){
169 size = size/1024;
170 units = 'GB';
171 }
172 return size.toFixed(2)+units;
173}
174
175function fileupload(uploadsize,filesz){ //更新上传进度,uploadsize为已上传大小,filesz为当前分片上传大小,单位B
176var nt = new Date().getTime();//获取当前时间
177 var pertime = (nt-ot)/1000; //计算出上次调用该方法时到现在的时间差,单位为s
178 ot = new Date().getTime(); //重新赋值时间,用于下次计算
179 //上传速度计算
180 var speed = filesz/pertime;
181 var bspeed = speed;
182 var units = 'B/s';//单位名称
183 if(speed/1024>1){
184 speed = speed/1024;
185 units = 'KB/s';
186 }
187 if(speed/1024>1){
188 speed = speed/1024;
189 units = 'MB/s';
190 }
191 speed = speed.toFixed(2);
192 upxcsize=bspeed;
193 var resttime = ((file.size-uploadsize)/bspeed).toFixed(2);
194 var bfb=Math.round(uploadsize / file.size * 10000) / 100 + "%";
195 if(uploadsfdyc<1){
196 var msg='文件上传中,共'+sizedwhs(file.size)+',已上传'+sizedwhs(uploadsize)+',剩余'+sizedwhs(file.size-uploadsize)+'未上传,当前速度获取中,预计剩余时间获取中,文件上传进度'+bfb;
197 }else{
198 var msg='文件上传中,共'+sizedwhs(file.size)+',已上传'+sizedwhs(uploadsize)+',剩余'+sizedwhs(file.size-uploadsize)+'未上传,当前速度'+speed+units+',预计剩余时间'+resttime+'秒,文件上传进度'+bfb;
199 }
200 msloadingup(msg);
201}