ajaxfileupload.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. var oldElement = jQuery('#' + fileElementId);
  27. var newElement = jQuery(oldElement).clone();
  28. jQuery(oldElement).attr('id', fileId);
  29. jQuery(oldElement).before(newElement);
  30. jQuery(oldElement).appendTo(form);
  31. //set attributes
  32. jQuery(form).css('position', 'absolute');
  33. jQuery(form).css('top', '-1200px');
  34. jQuery(form).css('left', '-1200px');
  35. jQuery(form).appendTo('body');
  36. //自定义参数
  37. if (data) {
  38. for (var i in data) {
  39. $('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  40. }
  41. }
  42. return form;
  43. },
  44. ajaxFileUpload: function(s) {
  45. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  46. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  47. var id = new Date().getTime()
  48. var form = jQuery.createUploadForm(id, s.fileElementId, s.data);
  49. var io = jQuery.createUploadIframe(id, s.secureuri);
  50. var frameId = 'jUploadFrame' + id;
  51. var formId = 'jUploadForm' + id;
  52. // Watch for a new set of requests
  53. if ( s.global && ! jQuery.active++ )
  54. {
  55. jQuery.event.trigger( "ajaxStart" );
  56. }
  57. var requestDone = false;
  58. // Create the request object
  59. var xml = {}
  60. if ( s.global )
  61. jQuery.event.trigger("ajaxSend", [xml, s]);
  62. // Wait for a response to come back
  63. var uploadCallback = function(isTimeout)
  64. {
  65. var io = document.getElementById(frameId);
  66. try
  67. {
  68. if(io.contentWindow)
  69. {
  70. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  71. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  72. }else if(io.contentDocument)
  73. {
  74. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  75. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  76. }
  77. }catch(e)
  78. {
  79. jQuery.handleError(s, xml, null, e);
  80. }
  81. if ( xml || isTimeout == "timeout")
  82. {
  83. requestDone = true;
  84. var status;
  85. try {
  86. status = isTimeout != "timeout" ? "success" : "error";
  87. // Make sure that the request was successful or notmodified
  88. if ( status != "error" )
  89. {
  90. // process the data (runs the xml through httpData regardless of callback)
  91. var data = jQuery.uploadHttpData( xml, s.dataType );
  92. // If a local callback was specified, fire it and pass it the data
  93. if ( s.success )
  94. s.success( data, status );
  95. // Fire the global callback
  96. if( s.global )
  97. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  98. } else
  99. jQuery.handleError(s, xml, status);
  100. } catch(e)
  101. {
  102. status = "error";
  103. jQuery.handleError(s, xml, status, e);
  104. }
  105. // The request was completed
  106. if( s.global )
  107. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  108. // Handle the global AJAX counter
  109. if ( s.global && ! --jQuery.active )
  110. jQuery.event.trigger( "ajaxStop" );
  111. // Process result
  112. if ( s.complete )
  113. s.complete(xml, status);
  114. jQuery(io).unbind()
  115. setTimeout(function()
  116. { try
  117. {
  118. jQuery(io).remove();
  119. jQuery(form).remove();
  120. } catch(e)
  121. {
  122. jQuery.handleError(s, xml, null, e);
  123. }
  124. }, 100)
  125. xml = null
  126. }
  127. }
  128. // Timeout checker
  129. if ( s.timeout > 0 )
  130. {
  131. setTimeout(function(){
  132. // Check to see if the request is still happening
  133. if( !requestDone ) uploadCallback( "timeout" );
  134. }, s.timeout);
  135. }
  136. try
  137. {
  138. var form = jQuery('#' + formId);
  139. jQuery(form).attr('action', s.url);
  140. jQuery(form).attr('method', 'POST');
  141. jQuery(form).attr('target', frameId);
  142. if(form.encoding)
  143. {
  144. jQuery(form).attr('encoding', 'multipart/form-data');
  145. }
  146. else
  147. {
  148. jQuery(form).attr('enctype', 'multipart/form-data');
  149. }
  150. jQuery(form).submit();
  151. } catch(e)
  152. {
  153. jQuery.handleError(s, xml, null, e);
  154. }
  155. jQuery('#' + frameId).load(uploadCallback );
  156. return {abort: function () {}};
  157. },
  158. uploadHttpData: function( r, type ) {
  159. var data = !type;
  160. data = type == "xml" || data ? r.responseXML : r.responseText;
  161. // If the type is "script", eval it in global context
  162. if ( type == "script" )
  163. jQuery.globalEval( data );
  164. // Get the JavaScript object, if JSON is used.
  165. if ( type == "json" )
  166. eval( "data = " + data );
  167. // evaluate scripts within html
  168. if ( type == "html" )
  169. jQuery("<div>").html(data).evalScripts();
  170. return data;
  171. },
  172. handleError: function( s, xhr, status, e ) {
  173. if ( s.error ) {
  174. s.error.call( s.context || window, xhr, status, e );
  175. }
  176. if ( s.global ) {
  177. (s.context ? jQuery(s.context) : jQuery.event).trigger ( "ajaxError", [xhr, s, e] );
  178. }
  179. }
  180. })