📄 upload.js
字号:
Upload.Form.reset();
// remove & reset
ofu.RemoveEvent();
Events.RemoveEvent(ofu.fileUpload, 'change', getName);
ofu.fileUpload.style.left = '-1000px';
delete ofu;
Upload.UploadFile();
Upload.oFileUpload.Reset();
if (!Upload.Uploading) Upload.SetUploadAble(true);
}
Events.AttachEvent(ofu.fileUpload, 'change', getName);
// 弹出文件上传选择框
/*
if (!this.FirstRun) {
this.FirstRun = true;
ofu.fileUpload.click();
}
*/
return ofu;
};
Upload.SetUploadAble = function (able) {
$('but_upload') && ($('but_upload').disabled = !able);
}
// 添加删除按钮
Upload.AddDeleteButton = function (name) {
var del = oNode.CreateNode('a');
oNode.AddNode(del, Upload.Files[name]['file_status']);
CSS.AddClass(del, 'delete');
del.href = 'javascript:void(0);';
del.title = '删除';
del.innerHTML = '删除';
Events.AttachEvent(del, 'click', function () {
Upload.RemoveOne(name);
Upload.oFileUpload.Reset();
});
}
// 执行上传,外部调用
Upload.Submit = function () {
if (this.Uploading) return;
this.Uploading = true;
this.SetUploadAble(false);
if (this.Indexs.length == 0) return;
this._Submit();
};
// 执行上传
Upload._Submit = function () {
// 判断是否已上传完毕或已被删除
for (var i = this.CurIndex, len = this.Indexs.length; i < len; i++) {
var curFile = this.Files[this.Indexs[i]];
if (curFile.finished || curFile.deleted) {
continue;
}
else {
this.CurIndex = i;
break;
}
}
if (i == len) {
this.Uploading = false;
if (this.ErrNum == 0)
this.RemoveAll(true);
return;
}
var item = this.Files[this.Indexs[this.CurIndex]];
// (1)拷贝当前文本域于表单中
var fileUpload = curFile.file_upload;
var parent = fileUpload.parentNode;
parent.removeChild(fileUpload);
this.Form.appendChild(fileUpload);
// 提交
this.Form.action = this.UploadUrl + '?type=' + this.Type + '&sessionid=' + this.Indexs[this.CurIndex] + '&filename=' + item.filename + Upload.DirectoryID + Upload.Path + (item.iscover ? '&iscover=true' : '') + (item.isrename ? '&isrename=true' : '') + '&' + this.QueryString;
//this.Form.elements['sign'].value = this.Indexs[this.CurIndex];
this.Form.submit();
this.Process();
item.file_item.focus();
item.file_icon.style.backgroundPosition = '-96px 0';
// (2)重设(1)操作的上传文件域到初始位置
this.Form.removeChild(fileUpload);
parent.appendChild(fileUpload);
this.oFileUpload.Reset();
};
// 显示上传进度
Upload.Process = function () {
var ajax = this.Ajax = new oAjax();
ajax.url = this.AjaxUrl + '?sessionid=' + this.Indexs[this.CurIndex] + '&rnd=' + Math.random() + '&' + this.QueryString;
//ajax.action = 'post';
//ajax.content = '';
ajax.callback = function (content) {
//alert(ajax.url + ' ' + content);
Upload.Operate(content);
delete ajax;
};
ajax.send();
};
// 显示进度,并继续调用ajax
Upload.Operate = function (content) {
var finished = false;
var err = false;
var name = this.Indexs[this.CurIndex];
// content: '进度|fileid|filesize|icon|status_info'
if (content.indexOf('|') > -1) {
content = content.split('|');
}
var length = parseInt((typeof(content[0]) != 'undefined' ? content[0] : content) || '0');
// 更新当前上传文件进度
// ...
// finished = true | false;
var item = this.Files[name];
// 进度
item.processing.style.width = (length > 0 ? length : 0) + '%';
// 文件大小
if (typeof content == 'object' && content[2])
item.file_size.innerHTML = content[2] + ' (' + length + '%)';
else
item.file_size.innerHTML = length + '%';
// 文件上传状态
item.file_status.innerHTML = '正在上传...';
if (length == 100) {
if (typeof content == 'object' && content[2])
item.file_size.innerHTML = content[2];
finished = true;
}
else if (length == -1) {
this.ErrNum++;
item.file_size.innerHTML = '';
err = true;
}
// 如果完成,执行下一个文件上传
if (finished || err) {
var _results = null;
if (this.CurIndex < this.Indexs.length - 1) {
this.CurIndex++;
this.Count--;
_results = Upload.UploadedOne(item.filename, content, item);
_results && (this.Count++);
// 上传下一个文件,当前函数跳出
if (!err && !_results) {
item.file_status.innerHTML = '完成';
this._Submit();
return;
}
}
else {
this.CurIndex = 0;
this.Uploading = false;
}
var halt = deal(_results);
if (halt) return;
if (!this.Uploading && this.ErrNum == 0) {
//Widget.Success('上传成功', '上传成功');
this.RemoveAll(true);
}
}
else {
Upload.Interval = setTimeout(function () {Upload.Process()}, 2000);
}
// 处理:错误|中断|完成
// results : {Info:'', Alert:function () {}}
function deal (results) {
item.file_status.innerHTML = results ? '' : (finished ? '完成' : '');
if (err || results) {
Upload.AddDeleteButton(name);
item.file_size.innerHTML = '错误:' + (results ? results.Info : (content[4] || ''));
if (results)
results.Alert();
else
Widget.Error('上传失败', '错误:' + (content[4] || ''));
// 当前文件重试
if (Upload.CurInde > 0) Upload.CurIndex--;
if (Upload.ErrNum > 0) Upload.ErrNum--;
// 出错则停止上传
Upload.Uploading = false;
item.file_icon.style.backgroundPosition = '-144px 0';
Upload.SetUploadAble(true);
return true;
}
else {
// 文件列表中最后一个文件上传完毕在此处判断
Upload.Count--;
var _results = Upload.UploadedOne(item.filename, content, item);
// 上传完某一文件,判断返回值
if (_results) {
Upload.Count++;
return deal(_results);
}
else {
item.finished = true;
item.file_icon.style.backgroundPosition = '-48px 0';
}
return false;
}
}
};
// 从上传对话框中选择一个文件后
// return
// -1 : 文件重复
// 0 : 超过允许的大小
// 1 : 正常
Upload.SelectedOne = function () {return 1};
// 渲染上传框时调用
Upload.Render = function () {};
// 删除上传队列中的一个文件
Upload.DeletedOne = function () {};
// 上传完单个文件调用
Upload.UploadedOne = function (filename, content, item) {return false};
// 上传完所有文件调用
Upload.UploadedAll = function (closeable) {};
// 移除某一个文件,并删除关联的数组及对象
Upload.RemoveOne = function (name) {
var index = this.Files[name].index;
//this.Indexs.splice(index, 1);
this.Files[name].deleted = true;
this.RemoveFinished(name, true);
this.Count--;
var count = 0;
for (var i = 0, len = this.Indexs.length; i < len; i++) {
name = this.Indexs[i];
if (!this.Files[name].deleted) {
count++;
}
}
if (count == 0) {
oNode.AddNode(this.TipInfo, $('item_container'));
Upload.SetUploadAble(false);
}
// render
Upload.Render();
this.DeletedOne(count);
}
// 移除已传完的文件
Upload.RemoveFinished = function (name, preserve) {
var item = this.Files[name];
//oNode.RemoveNode(item['frame'], item['frame'].parentNode);
oNode.RemoveNode(item['file_upload'], item['file_upload'].parentNode);
oNode.RemoveNode(item['file_item'], item['file_item'].parentNode);
!preserve && delete this.Files[name];
};
// 移除所有上传文
Upload.RemoveAll = function (closeable) {
clearTimeout(this.Interval);
var count = 0;
for (var name in this.Files) {
this.RemoveFinished(name);
count++;
}
this.Files = {};
this.Indexs = [];
this.CurIndex = 0;
this.Count = 0;
if (this.Uploading) {
this.Uploading = false;
this.Frame.src = 'about:blank';
}
(this.TipInfo && !$('empty')) && this.TipInfo.parentNode != $('item_container') && oNode.AddNode(this.TipInfo, $('item_container'));
this.TipInfo = $('empty');
if (count > 0) this.oFileUpload.Reset();
this.UploadedAll(closeable);
this.SetUploadAble(false);
//alert($('item_container').innerHTML);
// render
Upload.Render();
};
// 显示
Upload.Show = function () {
if (this.IsHide) {
this.IsHide = false;
Widget.Show();
}
};
// 隐藏
Upload.Hide = function () {
this.IsHide = true;
Widget.Hide();
};
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -