ajaxfileupload.js 6.4 KB

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