ExtJS+Jquery实现文件上传

  1. jQuery.extend({
  2. createUploadIframe:function(id,uri){
  3. varframeId='jQuery'+id;
  4. if(window.ActiveXObject){
  5. vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
  6. if(typeofuri=='boolean'){
  7. io.src='javascript:false';
  8. }
  9. elseif(typeofuri=='string'){
  10. io.src=uri;
  11. }
  12. }
  13. else{
  14. vario=document.createElement('iframe');
  15. io.id=frameId;
  16. io.name=frameId;
  17. }
  18. io.style.position='absolute';
  19. io.style.top='-1000px';
  20. io.style.left='-1000px';
  21. document.body.appendChild(io);
  22. returnio
  23. },
  24. ajaxUpload:function(s){
  25. //TODOintroduceglobalsettings,allowingtheclienttomodifythemforallrequests,notonlytimeout
  26. s=jQuery.extend({},jQuery.ajaxSettings,s);
  27. varid=newDate().getTime()
  28. io=jQuery.createUploadIframe(id,s.secureuri)
  29. //Watchforanewsetofrequests
  30. if(s.global&&!jQuery.active++)
  31. jQuery.event.trigger("ajaxStart");
  32. varrequestDone=false;
  33. //Createtherequestobject
  34. varxml={}
  35. if(s.global)
  36. jQuery.event.trigger("ajaxSend",[xml,s]);
  37. //Waitforaresponsetocomeback
  38. varuploadCallback=function(isTimeout){
  39. try{
  40. xml.responseText=io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  41. xml.responseXML=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  42. }
  43. catch(e){}
  44. if(xml||isTimeout=="timeout"){
  45. requestDone=true;
  46. varstatus;
  47. try{
  48. status=isTimeout!="timeout"?"success":"error";
  49. //Makesurethattherequestwassuccessfulornotmodified
  50. if(status!="error"){
  51. //processthedata(runsthexmlthroughhttpDataregardlessofcallback)
  52. vardata=jQuery.uploadHttpData(xml,s.dataType);
  53. //Ifalocalcallbackwasspecified,fireitandpassitthedata
  54. if(s.success)
  55. s.success(data,status);
  56. //Firetheglobalcallback
  57. if(s.global)
  58. jQuery.event.trigger("ajaxSuccess",[xml,s]);
  59. }else
  60. jQuery.handleError(s,xml,status);
  61. }catch(e){
  62. status="error";
  63. jQuery.handleError(s,xml,status,e);
  64. }
  65. //Therequestwascompleted
  66. if(s.global)
  67. jQuery.event.trigger("ajaxComplete",[xml,s]);
  68. //HandletheglobalAJAXcounter
  69. if(s.global&&!--jQuery.active)
  70. jQuery.event.trigger("ajaxStop");
  71. //Processresult
  72. if(s.complete)
  73. s.complete(xml,status);
  74. //jQuery(io).unbind()
  75. //setTimeout(function(){document.body.removeChild(io);},100)
  76. xml=null
  77. }
  78. }
  79. //Timeoutchecker
  80. if(s.timeout>0){
  81. setTimeout(function(){
  82. //Checktoseeiftherequestisstillhappening
  83. if(!requestDone)uploadCallback("timeout");
  84. },s.timeout);
  85. }
  86. try{
  87. varframeId='jQuery'+id;
  88. vario=document.getElementById(frameId);
  89. //InitializetheHTMLformpropertiesincasetheyare
  90. //notdefinedintheHTMLform.
  91. s.uploadform.action=s.url;
  92. s.uploadform.method='POST';
  93. s.uploadform.target=frameId;
  94. //Addextradatathatmayhavebeenalreadypassed.
  95. if(s.data){
  96. varoEls=s.data.split('&');
  97. for(vari=0;i<oEls.length;i++){
  98. varthisEl=oEls[i].split('=');
  99. varthisFormEl=document.createElement('input');
  100. thisFormEl.type='hidden';
  101. thisFormEl.name=thisEl[0];
  102. thisFormEl.value=thisEl[1];
  103. s.uploadform.appendChild(thisFormEl);
  104. }
  105. }
  106. if(s.uploadform.encoding){
  107. //IEdoesnotrespectpropertyenctypeforHTMLforms.
  108. //Insteadusepropertyencoding.
  109. s.uploadform.encoding='multipart/form-data';
  110. }
  111. else{
  112. s.uploadform.enctype='multipart/form-data';
  113. }
  114. s.uploadform.submit();
  115. }catch(e){
  116. jQuery.handleError(s,xml,null,e);
  117. }
  118. if(window.attachEvent){
  119. io.attachEvent('onload',uploadCallback);
  120. }
  121. else{
  122. io.addEventListener('load',uploadCallback,false);
  123. }
  124. return{abort:function(){}};
  125. },
  126. uploadHttpData:function(r,type){
  127. vardata=!type;
  128. data=type=="xml"||data?r.responseXML:r.responseText;
  129. //Ifthetypeis"script",evalitinglobalcontext
  130. if(type=="script")
  131. jQuery.globalEval(data);
  132. //GettheJavaScriptobject,ifJSONisused.
  133. if(type=="json")
  134. eval("data="+data);
  135. //evaluatescriptswithinhtml
  136. if(type=="html")
  137. jQuery("<div>").html(data).evalScripts();
  138. returndata;
  139. }
  140. })
  141. Ext.lib.Ajax.formRequest=function(form,uri,cb,data,isUpload,sslUri){
  142. varcreateComplete=function(cb){
  143. returnfunction(xhr,status){
  144. if((status=='error'||status=='timeout')&&cb.failure){
  145. cb.failure.call(cb.scope||window,{
  146. responseText:xhr.responseText,
  147. responseXML:xhr.responseXML,
  148. argument:cb.argument
  149. });
  150. }elseif(cb.success){
  151. cb.success.call(cb.scope||window,{
  152. responseText:xhr.responseText,
  153. responseXML:xhr.responseXML,
  154. argument:cb.argument
  155. });
  156. }
  157. };
  158. };
  159. if(isUpload){
  160. jQuery.ajaxUpload({
  161. uploadform:form,
  162. data:data,
  163. url:uri,
  164. secureuri:sslUri,
  165. complete:createComplete(cb)
  166. });
  167. }else{
  168. jQuery.ajax({
  169. type:Ext.getDom(form).method||'POST',
  170. url:uri,
  171. data:jQuery(form).formSerialize()+(data?'&'+data:''),
  172. timeout:cb.timeout,
  173. complete:createComplete(cb)
  174. });
  175. }
  176. };

你可能感兴趣的:(jquery)