/* ! 宝塔文件上传组件 License https://www.bt.cn By 黄文良 <287962566@qq.com> */ var bt_upload_file = { f: null, f_total: 0, f_path: null, split_size: 1024 * 1024 * 2, _files: [], _error: 0, _start_time: 0, _t_start_time: 0, collback_to: null, upload_url: '/files?action=upload', _loadT: null, open: function (path, is_exts, ps, collback_to) { bt_upload_file.f_path = path.replace('//', '/'); user_agent = navigator.userAgent.toLowerCase(); var btn_dir = ''; var accept_ext = ''; if (!is_exts) { if (user_agent.indexOf('chrome') !== -1 || user_agent.indexOf('firefox') !== -1) { btn_dir = '' } } else { accept_ext = 'accept="' + is_exts + '"' } var other_ps = ''; if (ps) { other_ps = ' --- ' + ps + '' } else { other_ps = ' --- 支持断点续传' } if (collback_to) { bt_upload_file.collback_to = collback_to } bt_upload_file._loadT = layer.open({ type: 1, title: '上传文件到[' + bt_upload_file.f_path + ']' + other_ps, area: ['550px', '500px'], shadeClose: false, closeBtn: 2, content: '
已上传(' + i + '/' + len + '),' + total_time + '
'); if (len <= i) { $("#totalProgress").html('上传完成(' + i + '/' + len + '), ' + total_time + '
'); bt_upload_file._files = []; $("#filesClose,#up,#opt").removeAttr("disabled"); if (bt_upload_file.collback_to) { bt_upload_file.collback_to(bt_upload_file.f_path) } return false } if (i > 10) { $("#up_box").scrollTop(35 * (i - 10) + 50) } bt_upload_file._start_time = new Date(); bt_upload_file._error = 0; bt_upload_file.f = bt_upload_file._files[i]; bt_upload_file.f_total = Math.ceil(bt_upload_file.f.size / bt_upload_file.split_size); bt_upload_file.upload(0, i) }, upload: function (start, i) { var end = Math.min(bt_upload_file.f.size, start + bt_upload_file.split_size); var len = bt_upload_file._files.length; var f_path = bt_upload_file.f_path; if (bt_upload_file.f.webkitRelativePath) { f_path = bt_upload_file.f_path + '/' + bt_upload_file.f.webkitRelativePath.replace('/' + bt_upload_file.f.name, '') } var form = new FormData(); form.append("f_path", f_path.replace('//', '/')); form.append("f_name", bt_upload_file.f.name); form.append("f_size", bt_upload_file.f.size); form.append("f_start", start); form.append("blob", bt_upload_file.f.slice(start, end)); $.ajax({ url: bt_upload_file.upload_url, type: "POST", data: form, async: true, processData: false, contentType: false, success: function (data) { if (typeof (data) === "number") { var progress = parseInt(data / bt_upload_file.f.size * 100); var total_time = bt_upload_file.diff_time(bt_upload_file._t_start_time, new Date()); $("#totalProgress").html('已上传(' + i + '/' + len + '),' + total_time + '
'); $("#up_box li em")[i].outerHTML = '上传进度:' + progress + '%'; $("#up_box li .filesize")[i].outerHTML = '' + bt_upload_file.to_size(data) + '/' + bt_upload_file.to_size(bt_upload_file.f.size) + ''; $("#up_box li em")[i].focus(); bt_upload_file.upload(data, i) } else { if (data.status) { var f_time = bt_upload_file.diff_time(bt_upload_file._start_time, new Date()); $("#up_box li em")[i].outerHTML = '已完成(' + f_time + ')'; $("#up_box li .filesize")[i].outerHTML = '' + bt_upload_file.to_size(bt_upload_file.f.size) + '/' + bt_upload_file.to_size(bt_upload_file.f.size) + '' } else { $("#up_box li em")[i].outerHTML = '' + data.msg + '' } bt_upload_file.start(i + 1) } }, error: function (e) { if (bt_upload_file._error > 5) { $("#up_box li em")[i].outerHTML = '上传失败'; bt_upload_file.start(i + 1); return } bt_upload_file._error += 1; bt_upload_file.upload(start, i) } }) }, diff_time: function (start_date, end_date) { var diff = end_date.getTime() - start_date.getTime(); var minutes = Math.floor(diff / (60 * 1000)); var leave3 = diff % (60 * 1000); var seconds = (leave3 / 1000).toFixed(2); var result = seconds + '秒'; if (minutes > 0) { result = minutes + "分" + result } return result } };