Added a Jee Ajax upload class
This commit is contained in:
parent
5d504a3a59
commit
542ee5a3cb
13 changed files with 532 additions and 2 deletions
97
src/zutil/jee/upload/AjaxFileUpload.js
Normal file
97
src/zutil/jee/upload/AjaxFileUpload.js
Normal file
|
|
@ -0,0 +1,97 @@
|
|||
/* Values:
|
||||
* Servlet url(String) = SERVLET_URL
|
||||
* Background upload(boolean) = BGUPLOAD
|
||||
* Queue item HTML(String) = PROGHTML
|
||||
*/
|
||||
var upload_index = 0;
|
||||
var upload_update = false;
|
||||
|
||||
// Autostart
|
||||
jQuery(document).ready(function(){
|
||||
initUpload();
|
||||
updateUploadStatus()
|
||||
});
|
||||
|
||||
/* Initiates a new upload */
|
||||
function initUpload(){
|
||||
var name = "uploadFrame_"+upload_index;
|
||||
|
||||
// Add iframe
|
||||
jQuery("body").append("<iframe id='"+name+"' name='"+name+"' height='0' " +
|
||||
"width='0' frameborder='0' scrolling='yes' src='about:blank'></iframe>");
|
||||
|
||||
// Init form settings
|
||||
var form = jQuery("#AjaxFileUpload");
|
||||
//form.attr("encoding", "multipart/form-data");
|
||||
form.attr("enctype", "multipart/form-data");
|
||||
form.attr("method", "post");
|
||||
form.attr("target", name);
|
||||
form.attr("action", "{SERVLET_URL}");
|
||||
form.bind('submit', startUpload );
|
||||
//form.attr("onSubmit", "startUpload()");
|
||||
|
||||
// reset the form
|
||||
jQuery("#AjaxFileUpload").each(function(){
|
||||
this.reset();
|
||||
});
|
||||
|
||||
upload_index++;
|
||||
}
|
||||
|
||||
function startUpload(){
|
||||
if(!upload_update)
|
||||
setTimeout("updateUploadStatus()", 500);
|
||||
|
||||
// Init new upload
|
||||
setTimeout("initUpload()", 500);
|
||||
}
|
||||
|
||||
|
||||
function updateUploadStatus(){
|
||||
jQuery.ajax({
|
||||
url: "{SERVLET_URL}",
|
||||
cache: false,
|
||||
dataType: 'json',
|
||||
success: function(data){
|
||||
// Update
|
||||
upload_update = true;
|
||||
if(data == null || data.length == 0){
|
||||
upload_update = false;
|
||||
}
|
||||
else setTimeout("updateUploadStatus()", 1000);
|
||||
|
||||
// Request upload info
|
||||
jQuery.each(data, function(index,item){
|
||||
// add new list item if needed
|
||||
if( jQuery("#UploadQueue #"+item.id).size() == 0){
|
||||
$("#UploadQueue").append("<li id='"+item.id+"'>{PROGHTML}</li>");
|
||||
}
|
||||
// Update data
|
||||
if(jQuery("#UploadQueue #"+item.id+" .filename").size() > 0)
|
||||
jQuery("#UploadQueue #"+item.id+" .filename").html( item.filename );
|
||||
if(jQuery("#UploadQueue #"+item.id+" .progress").size() > 0)
|
||||
jQuery("#UploadQueue #"+item.id+" .progress").animate({width: item.percent+"%"}, 'slow');
|
||||
//jQuery("#UploadQueue #"+item.id+" #progress").css("width", item.percent+"%");
|
||||
if(jQuery("#UploadQueue #"+item.id+" .total").size() > 0)
|
||||
jQuery("#UploadQueue #"+item.id+" .total").html( item.total );
|
||||
if(jQuery("#UploadQueue #"+item.id+" .uploaded").size() > 0)
|
||||
jQuery("#UploadQueue #"+item.id+" .uploaded").html( item.uploaded );
|
||||
if(jQuery("#UploadQueue #"+item.id+" .speed").size() > 0)
|
||||
jQuery("#UploadQueue #"+item.id+" .speed").html( item.speed );
|
||||
if(jQuery("#UploadQueue #"+item.id+" .status").size() > 0)
|
||||
jQuery("#UploadQueue #"+item.id+" .status").html( item.status );
|
||||
|
||||
// remove li when done
|
||||
if( item.status == "Done" ){
|
||||
jQuery("#UploadQueue #"+item.id).delay(5000).fadeOut("slow", function(){
|
||||
jQuery(this).remove();
|
||||
});
|
||||
//jQuery("#UploadQueue #"+item.id).attr("id", "del");
|
||||
}
|
||||
});
|
||||
},
|
||||
error: function(request, textStatus){
|
||||
alert(textStatus);
|
||||
}
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue