ajaxfileupload.js 6.8 KB

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