浏览代码

upload progress

master
Pascal Gloor 5 年前
父节点
当前提交
95a0e7e92d
共有 1 个文件被更改,包括 32 次插入33 次删除
  1. 32
    33
      www/js/autodoc.js

+ 32
- 33
www/js/autodoc.js 查看文件

}); });
} }
else { else {
for(var id=0; id<temp.upload.length; id++) {
for (var id = 0; id < temp.upload.length; id++) {
var file = temp.upload[id]; var file = temp.upload[id];


if ( file.progress != 100 ) {
if ( file.progress == 0 ) {
if (file.progress != 100) {
if (file.progress == 0) {
if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') { if (file.obj.type != 'application/pdf' && file.obj.type != 'image/png' && file.obj.type != 'image/jpeg') {
file['progress'] = 100; file['progress'] = 100;
file['error'] = 'invalid file type'; file['error'] = 'invalid file type';
var reader = new FileReader(); var reader = new FileReader();
file['progress'] = 1; file['progress'] = 1;


$(reader).on('load', id, upload_read_event);
$(reader).on('load', id, upload_read_event);
$(reader).on('loadstart', id, upload_read_event); $(reader).on('loadstart', id, upload_read_event);
$(reader).on('loadend', id, upload_read_event);
$(reader).on('progress', id, upload_read_event);
$(reader).on('error', id, upload_read_event);
$(reader).on('abort', id, upload_read_event);
$(reader).on('loadend', id, upload_read_event);
$(reader).on('progress', id, upload_read_event);
$(reader).on('error', id, upload_read_event);
$(reader).on('abort', id, upload_read_event);


reader.readAsArrayBuffer(file.obj); reader.readAsArrayBuffer(file.obj);
} }
} }
} }
} }
setTimeout(function() { upload_handler(); }, 1000);
setTimeout(function () { upload_handler(); }, 1000);
} }


function upload_read_event(e) { function upload_read_event(e) {
if ( e.type == 'progress' ) {
if ( e.lengthComputable ) {
var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
if ( progress < 1 ) { progress = 1; }
if ( progress > 49 ) { progress = 49; }
temp.upload[e.data]['progress'] = progress;
if (e.type == 'progress') {
if (e.lengthComputable) {
var progress = Math.floor(e.originalEvent.loaded / e.originalEvent.total * 50);
if (progress < 1) { progress = 1; }
if (progress > 49) { progress = 49; }
temp.upload[e.data]['progress'] = progress;
} }
} }
else if ( e.type == 'load' ) {
else if (e.type == 'load') {
temp.upload[e.data]['progress'] = 50; temp.upload[e.data]['progress'] = 50;
console.log(e.target.result); console.log(e.target.result);
$.ajax({ $.ajax({
contentType: temp.upload[e.data].obj.type, contentType: temp.upload[e.data].obj.type,
context: e.data, context: e.data,
processData: false, processData: false,
xhr: function()
{
xhr: function () {
var xhr = new window.XMLHttpRequest(); var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt){
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
$(xhr.upload).on('progress', e.data, function (evt, id) {

if (evt.lengthComputable) {
var progress = Math.floor(evt.loaded / evt.total * 50);
if (progress < 1) { progress = 1; }
if (progress > 49) { progress = 49; }
temp.upload[id].progress = 50 + progress;
console.log("upload_progress_event", evt); console.log("upload_progress_event", evt);
} }
}, false);
});
return xhr; return xhr;
}, },
success: function(data, status, xhr){
console.log("upload_complete",data, status, xhr);
success: function (data, status, xhr) {
console.log("upload_complete", data, status, xhr);
} }
}); });


} }


} }


function upload_status() { function upload_status() {
// console.log("upload_status");
$.each(temp.upload, function (id, file) { $.each(temp.upload, function (id, file) {
if (!$('.autodoc_upload_status_' + id).length) { if (!$('.autodoc_upload_status_' + id).length) {
console.log("creating element");
var obj = $(".autodoc_template_progress").children().clone(); var obj = $(".autodoc_template_progress").children().clone();
$(obj).addClass('autodoc_upload_status_'+id);
$(obj).addClass('autodoc_upload_status_' + id);
$('.autodoc_upload_status').append(obj); $('.autodoc_upload_status').append(obj);
} }
console.log($("upload elem", '.autodoc_upload_status_'+id));
var obj = $('.autodoc_upload_status_' + id).children(); var obj = $('.autodoc_upload_status_' + id).children();
console.log("upload child", obj);
if (file.error != null) { if (file.error != null) {
$(obj).html(file.obj.name + ': ' + file.error); $(obj).html(file.obj.name + ': ' + file.error);
$(obj).css('width', '100%'); $(obj).css('width', '100%');


} }
}); });
setTimeout(function() { upload_status(); }, 1000);
setTimeout(function () { upload_status(); }, 100);
} }

正在加载...
取消
保存