jquery.ajaxfileupload.js 3.4 KB

1
  1. jQuery.extend({createUploadIframe:function(a,d){var c="jUploadFrame"+a;if(window.ActiveXObject)if("9.0"==jQuery.browser.version||"10.0"==jQuery.browser.version){var b=document.createElement("iframe");b.id=c;b.name=c}else{if("6.0"==jQuery.browser.version||"7.0"==jQuery.browser.version||"8.0"==jQuery.browser.version)b=document.createElement('\x3ciframe id\x3d"'+c+'" name\x3d"'+c+'" /\x3e'),"boolean"==typeof d?b.src="javascript:false":"string"==typeof d&&(b.src=d)}else b=document.createElement("iframe"),b.id=c,b.name=c;b.style.position="absolute";b.style.top="-1000px";b.style.left="-1000px";document.body.appendChild(b);return b},createUploadForm:function(a,d,c){var b="jUploadForm"+a;a="jUploadFile"+a;b=$('\x3cform action\x3d"" method\x3d"POST" name\x3d"'+b+'" id\x3d"'+b+'" enctype\x3d"multipart/form-data"\x3e\x3c/form\x3e');d=$("#"+d);var g=$(d).clone(!0);$(d).attr("id",a);$(d).before(g);$(d).appendTo(b);if(c)for(var e in c)$('\x3cinput type\x3d"hidden" name\x3d"'+e+'" value\x3d"'+c[e]+'" /\x3e').appendTo(b);$(b).css("position","absolute");$(b).css("top","-1200px");$(b).css("left","-1200px");$(b).appendTo("body");return b},ajaxFileUpload:function(a){a=jQuery.extend({},jQuery.ajaxSettings,a);var d=(new Date).getTime(),c=jQuery.createUploadForm(d,a.fileElementId,a.data);jQuery.createUploadIframe(d,a.secureuri);var b="jUploadFrame"+d,d="jUploadForm"+d;a.global&&!jQuery.active++&&jQuery.event.trigger("ajaxStart");var g=!1,e={};a.global&&jQuery.event.trigger("ajaxSend",[e,a]);var k=function(d){var f=document.getElementById(b);try{f.contentWindow?(e.responseText=f.contentWindow.document.body?f.contentWindow.document.body.innerHTML:null,e.responseXML=f.contentWindow.document.XMLDocument?f.contentWindow.document.XMLDocument:f.contentWindow.document):f.contentDocument&&(e.responseText=f.contentDocument.document.body?f.contentDocument.document.body.innerHTML:null,e.responseXML=f.contentDocument.document.XMLDocument?f.contentDocument.document.XMLDocument:f.contentDocument.document)}catch(l){jQuery.handleError(a,e,null,l)}if(e||"timeout"==d){g=!0;var h;try{if(h="timeout"!=d?"success":"error","error"!=h){var k=jQuery.uploadHttpData(e,a.dataType);a.success&&a.success(k,h);a.global&&jQuery.event.trigger("ajaxSuccess",[e,a])}else jQuery.handleError(a,e,h)}catch(l){h="error",jQuery.handleError(a,e,h,l)}a.global&&jQuery.event.trigger("ajaxComplete",[e,a]);a.global&&!--jQuery.active&&jQuery.event.trigger("ajaxStop");a.complete&&a.complete(e,h);jQuery(f).unbind();setTimeout(function(){try{$(f).remove(),$(c).remove()}catch(b){jQuery.handleError(a,e,null,b)}},100);e=null}};0<a.timeout&&setTimeout(function(){g||k("timeout")},a.timeout);try{c=$("#"+d),$(c).attr("action",a.url),$(c).attr("method","POST"),$(c).attr("target",b),c.encoding?c.encoding="multipart/form-data":c.enctype="multipart/form-data",$(c).submit()}catch(m){jQuery.handleError(a,e,null,m)}window.attachEvent?document.getElementById(b).attachEvent("onload",k):document.getElementById(b).addEventListener("load",k,!1);return{abort:function(){}}},uploadHttpData:function(a,d){var c;c="xml"!=d&&d?a.responseText:a.responseXML;"script"==d&&jQuery.globalEval(c);if("json"==d){c=a.responseText;var b=c.indexOf("\x3e");if(-1!=b){var g=c.indexOf("\x3c",b+1);-1!=g&&(c=c.substring(b+1,g))}eval("data \x3d "+c)}return c},handleError:function(a,d,c,b){a.error&&a.error.call(a.context||a,d,c,b);a.global&&(a.context?jQuery(a.context):jQuery.event).trigger("ajaxError",[d,a,b])}});