ajaxfileupload.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. jQuery.extend({
  2. createUploadIframe: function(id, uri)
  3. {
  4. //create frame
  5. var frameId = 'jUploadFrame' + id;
  6. var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
  7. if(window.ActiveXObject)
  8. {
  9. if(typeof uri== 'boolean'){
  10. iframeHtml += ' src="' + 'javascript:false' + '"';
  11. }
  12. else if(typeof uri== 'string'){
  13. iframeHtml += ' src="' + uri + '"';
  14. }
  15. }
  16. iframeHtml += ' />';
  17. jQuery(iframeHtml).appendTo(document.body);
  18. return jQuery('#' + frameId).get(0);
  19. },
  20. createUploadForm: function(id, fileElementId, data)
  21. {
  22. //create form
  23. var formId = 'jUploadForm' + id;
  24. var fileId = 'jUploadFile' + id;
  25. var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  26. if(data)
  27. {
  28. for(var i in data)
  29. {
  30. jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  31. }
  32. }
  33. var oldElement = jQuery('#' + fileElementId);
  34. var newElement = jQuery(oldElement).clone(true);
  35. jQuery(newElement).val('');
  36. jQuery(oldElement).attr('id', fileId);
  37. jQuery(oldElement).before(newElement);
  38. jQuery(oldElement).appendTo(form);
  39. //set attributes
  40. jQuery(form).css('position', 'absolute');
  41. jQuery(form).css('top', '-1200px');
  42. jQuery(form).css('left', '-1200px');
  43. jQuery(form).appendTo('body');
  44. return form;
  45. },
  46. handleError: function( s, xhr, status, e ) {
  47. // If a local callback was specified, fire it
  48. var errorObj = eval('[' + xhr.responseText + ']')[0];
  49. if (xhr.status == 902 && errorObj) {
  50. //会话超时 定向之后 重新登录
  51. alert(errorObj.errorMsg);
  52. }
  53. if ( s.error ) {
  54. s.error.call( s.context || s, xhr, status, e );
  55. }
  56. // Fire the global callback
  57. if ( s.global ) {
  58. (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
  59. }
  60. },
  61. ajaxFileUpload: function(s) {
  62. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  63. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  64. var id = new Date().getTime()
  65. var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
  66. var io = jQuery.createUploadIframe(id, s.secureuri);
  67. var frameId = 'jUploadFrame' + id;
  68. var formId = 'jUploadForm' + id;
  69. // Watch for a new set of requests
  70. if ( s.global && ! jQuery.active++ )
  71. {
  72. jQuery.event.trigger( "ajaxStart" );
  73. }
  74. var requestDone = false;
  75. // Create the request object
  76. var xml = {}
  77. if ( s.global )
  78. jQuery.event.trigger("ajaxSend", [xml, s]);
  79. // Wait for a response to come back
  80. var uploadCallback = function(isTimeout)
  81. {
  82. var io = document.getElementById(frameId);
  83. try
  84. {
  85. if(io.contentWindow)
  86. {
  87. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  88. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  89. }else if(io.contentDocument)
  90. {
  91. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  92. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  93. }
  94. }catch(e)
  95. {
  96. jQuery.handleError(s, xml, null, e);
  97. }
  98. if ( xml || isTimeout == "timeout")
  99. {
  100. requestDone = true;
  101. var status;
  102. try {
  103. status = isTimeout != "timeout" ? "success" : "error";
  104. // Make sure that the request was successful or notmodified
  105. if ( status != "error" )
  106. {
  107. // process the data (runs the xml through httpData regardless of callback)
  108. var data = jQuery.uploadHttpData( xml, s.dataType );
  109. // If a local callback was specified, fire it and pass it the data
  110. if ( s.success )
  111. s.success( data, status );
  112. // Fire the global callback
  113. if( s.global )
  114. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  115. } else
  116. jQuery.handleError(s, xml, status);
  117. } catch(e)
  118. {
  119. status = "error";
  120. jQuery.handleError(s, xml, status, e);
  121. }
  122. // The request was completed
  123. if( s.global )
  124. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  125. // Handle the global AJAX counter
  126. if ( s.global && ! --jQuery.active )
  127. jQuery.event.trigger( "ajaxStop" );
  128. // Process result
  129. if ( s.complete )
  130. s.complete(xml, status);
  131. jQuery(io).unbind()
  132. setTimeout(function()
  133. { try
  134. {
  135. jQuery(io).remove();
  136. jQuery(form).remove();
  137. } catch(e)
  138. {
  139. jQuery.handleError(s, xml, null, e);
  140. }
  141. }, 100)
  142. xml = null
  143. }
  144. }
  145. // Timeout checker
  146. if ( s.timeout > 0 )
  147. {
  148. setTimeout(function(){
  149. // Check to see if the request is still happening
  150. if( !requestDone ) uploadCallback( "timeout" );
  151. }, s.timeout);
  152. }
  153. try
  154. {
  155. var form = jQuery('#' + formId);
  156. jQuery(form).attr('action', s.url);
  157. jQuery(form).attr('method', 'POST');
  158. jQuery(form).attr('target', frameId);
  159. if(form.encoding)
  160. {
  161. jQuery(form).attr('encoding', 'multipart/form-data');
  162. }
  163. else
  164. {
  165. jQuery(form).attr('enctype', 'multipart/form-data');
  166. }
  167. jQuery(form).submit();
  168. } catch(e)
  169. {
  170. jQuery.handleError(s, xml, null, e);
  171. }
  172. jQuery('#' + frameId).load(uploadCallback);
  173. return {abort: function () {}};
  174. },
  175. uploadHttpData: function( r, type ) {
  176. var data = !type;
  177. data = type == "xml" || data ? r.responseXML : r.responseText;
  178. data = data.replace("<pre>","");
  179. data = data.replace("</pre>","");
  180. // If the type is "script", eval it in global context
  181. if ( type == "script" )
  182. jQuery.globalEval( data );
  183. // Get the JavaScript object, if JSON is used.
  184. if ( type == "json" ){
  185. data = r.responseText;
  186. var start = data.indexOf(">");
  187. if(start != -1) {
  188. var end = data.indexOf("<", start + 1);
  189. if(end != -1) {
  190. data = data.substring(start + 1, end);
  191. }
  192. }
  193. eval( "data = " + data );
  194. if (data && data.errorCode == '902') {
  195. dialog({
  196. title:'提示',
  197. content:'<div class="menu_tips">'+data.errorMsg+'</div>',
  198. width:300,
  199. ok:function(){ },
  200. okValue:'确定',
  201. cancel:function(){},
  202. cancelValue:'取消'
  203. }).showModal();
  204. }
  205. }
  206. // evaluate scripts within html
  207. if ( type == "html" )
  208. jQuery("<div>").html(data).evalScripts();
  209. return data;
  210. }
  211. })