ajaxfileupload.js 7.8 KB

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