ExtJS+Jquery实现文件上传

 
  1. jQuery.extend({ 
  2. createUploadIframe: function(id, uri){ 
  3. var frameId = 'jQuery' + id; 
  4. if(window.ActiveXObject) { 
  5. var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />'); 
  6. if(typeof uri== 'boolean'){ 
  7. io.src = 'javascript:false'
  8. else if(typeof uri== 'string'){ 
  9. io.src = uri; 
  10. else { 
  11. var io = document.createElement('iframe'); 
  12. io.id = frameId; 
  13. io.name = frameId; 
  14. io.style.position = 'absolute'
  15. io.style.top = '-1000px'
  16. io.style.left = '-1000px'
  17. document.body.appendChild(io); 
  18. return io 
  19. }, 
  20. ajaxUpload: function(s) { 
  21. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout 
  22. s = jQuery.extend({}, jQuery.ajaxSettings, s); 
  23. var id = new Date().getTime() 
  24. io = jQuery.createUploadIframe(id, s.secureuri) 
  25. // Watch for a new set of requests 
  26. if ( s.global && ! jQuery.active++ ) 
  27. jQuery.event.trigger( "ajaxStart" ); 
  28. var requestDone = false
  29. // Create the request object 
  30. var xml = {} 
  31. if ( s.global ) 
  32. jQuery.event.trigger("ajaxSend", [xml, s]); 
  33. // Wait for a response to come back 
  34. var uploadCallback = function(isTimeout){ 
  35. try { 
  36. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null
  37. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document; 
  38. catch(e){} 
  39. if ( xml || isTimeout == "timeout") { 
  40. requestDone = true
  41. var status; 
  42. try { 
  43. status = isTimeout != "timeout" ? "success" : "error"
  44. // Make sure that the request was successful or notmodified 
  45. if ( status != "error" ) { 
  46. // process the data (runs the xml through httpData regardless of callback) 
  47. var data = jQuery.uploadHttpData( xml, s.dataType ); 
  48. // If a local callback was specified, fire it and pass it the data 
  49. if ( s.success ) 
  50. s.success( data, status ); 
  51. // Fire the global callback 
  52. if( s.global ) 
  53. jQuery.event.trigger( "ajaxSuccess", [xml, s] ); 
  54. else 
  55. jQuery.handleError(s, xml, status); 
  56. catch(e) { 
  57. status = "error"
  58. jQuery.handleError(s, xml, status, e); 
  59. // The request was completed 
  60. if( s.global ) 
  61. jQuery.event.trigger( "ajaxComplete", [xml, s] ); 
  62. // Handle the global AJAX counter 
  63. if ( s.global && ! --jQuery.active ) 
  64. jQuery.event.trigger( "ajaxStop" ); 
  65. // Process result 
  66. if ( s.complete ) 
  67. s.complete(xml, status); 
  68. // jQuery(io).unbind() 
  69. // setTimeout(function(){ document.body.removeChild(io); }, 100) 
  70. xml = null 
  71. // Timeout checker 
  72. if ( s.timeout > 0 ) { 
  73. setTimeout(function(){ 
  74. // Check to see if the request is still happening 
  75. if( !requestDone ) uploadCallback( "timeout" ); 
  76. }, s.timeout); 
  77. try { 
  78. var frameId = 'jQuery' + id; 
  79. var io = document.getElementById(frameId); 
  80. // Initialize the HTML form properties in case they are 
  81. // not defined in the HTML form. 
  82. s.uploadform.action = s.url; 
  83. s.uploadform.method = 'POST'
  84. s.uploadform.target = frameId; 
  85. // Add extra data that may have been already passed. 
  86. if (s.data) { 
  87. var oEls = s.data.split('&'); 
  88. for (var i=0; i < oEls.length; i++ ){ 
  89. var thisEl = oEls[i].split('='); 
  90. var thisFormEl = document.createElement('input'); 
  91. thisFormEl.type = 'hidden'
  92. thisFormEl.name = thisEl[0]; 
  93. thisFormEl.value = thisEl[1]; 
  94. s.uploadform.appendChild(thisFormEl); 
  95. if(s.uploadform.encoding){ 
  96. // IE does not respect property enctype for HTML forms. 
  97. // Instead use property encoding. 
  98. s.uploadform.encoding = 'multipart/form-data'
  99. else
  100. s.uploadform.enctype = 'multipart/form-data'
  101. s.uploadform.submit(); 
  102. catch(e) { 
  103. jQuery.handleError(s, xml, null, e); 
  104. if(window.attachEvent){ 
  105. io.attachEvent('onload', uploadCallback); 
  106. else
  107. io.addEventListener('load', uploadCallback, false); 
  108. return {abort: function () {}}; 
  109. }, 
  110. uploadHttpData: function( r, type ) { 
  111. var data = !type; 
  112. data = type == "xml" || data ? r.responseXML : r.responseText; 
  113. // If the type is "script", eval it in global context 
  114. if ( type == "script" ) 
  115. jQuery.globalEval( data ); 
  116. // Get the JavaScript object, if JSON is used. 
  117. if ( type == "json" ) 
  118. eval( "data = " + data ); 
  119. // evaluate scripts within html 
  120. if ( type == "html" ) 
  121. jQuery("<div>").html(data).evalScripts(); 
  122. return data; 
  123. }) 
  124. Ext.lib.Ajax.formRequest = function(form, uri, cb, data, isUpload, sslUri){ 
  125. var createComplete = function(cb){ 
  126. return function(xhr, status){ 
  127. if((status == 'error' || status == 'timeout') && cb.failure){ 
  128. cb.failure.call(cb.scope||window, { 
  129. responseText: xhr.responseText, 
  130. responseXML : xhr.responseXML, 
  131. argument: cb.argument 
  132. }); 
  133. }else if(cb.success){ 
  134. cb.success.call(cb.scope||window, { 
  135. responseText: xhr.responseText, 
  136. responseXML : xhr.responseXML, 
  137. argument: cb.argument 
  138. }); 
  139. }; 
  140. }; 
  141. if (isUpload) { 
  142. jQuery.ajaxUpload({ 
  143. uploadform: form, 
  144. data: data, 
  145. url: uri, 
  146. secureuri: sslUri, 
  147. complete: createComplete(cb) 
  148. }); 
  149. else { 
  150. jQuery.ajax({ 
  151. type: Ext.getDom(form).method ||'POST'
  152. url: uri, 
  153. data: jQuery(form).formSerialize()+(data?'&'+data:''), 
  154. timeout: cb.timeout, 
  155. complete: createComplete(cb) 
  156. }); 
  157. };  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值