ajaxfileupload.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // JavaScript Document
  2. jQuery.extend({
  3. createUploadIframe: function(id, uri)
  4. {
  5. //create frame
  6. var frameId = 'jUploadFrame' + id;
  7. if(window.ActiveXObject) {
  8. var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
  9. if(typeof uri== 'boolean'){
  10. io.src = 'javascript:false';
  11. }
  12. else if(typeof uri== 'string'){
  13. io.src = uri;
  14. }
  15. }
  16. else {
  17. var io = document.createElement('iframe');
  18. io.id = frameId;
  19. io.name = frameId;
  20. }
  21. io.style.position = 'absolute';
  22. io.style.top = '-1000px';
  23. io.style.left = '-1000px';
  24. document.body.appendChild(io);
  25. return io;
  26. },
  27. createUploadForm: function(id, fileElementId)
  28. {
  29. //create form
  30. var formId = 'jUploadForm' + id;
  31. var fileId = 'jUploadFile' + id;
  32. var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  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 = s.fileElementId;
  49. var form = jQuery.createUploadForm(id, s.fileElementId);
  50. var io = jQuery.createUploadIframe(id, s.secureuri);
  51. var frameId = 'jUploadFrame' + id;
  52. var formId = 'jUploadForm' + id;
  53. if( s.global && ! jQuery.active++ )
  54. {
  55. // Watch for a new set of requests
  56. jQuery.event.trigger( "ajaxStart" );
  57. }
  58. var requestDone = false;
  59. // Create the request object
  60. var xml = {};
  61. if( s.global )
  62. {
  63. jQuery.event.trigger("ajaxSend", [xml, s]);
  64. }
  65. var uploadCallback = function(isTimeout)
  66. {
  67. // Wait for a response to come back
  68. var io = document.getElementById(frameId);
  69. try
  70. {
  71. if(io.contentWindow)
  72. {
  73. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  74. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  75. }else if(io.contentDocument)
  76. {
  77. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  78. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  79. }
  80. }catch(e)
  81. {
  82. jQuery.handleError(s, xml, null, e);
  83. }
  84. if( xml || isTimeout == "timeout")
  85. {
  86. requestDone = true;
  87. var status;
  88. try {
  89. status = isTimeout != "timeout" ? "success" : "error";
  90. // Make sure that the request was successful or notmodified
  91. if( status != "error" )
  92. {
  93. // process the data (runs the xml through httpData regardless of callback)
  94. var data = jQuery.uploadHttpData( xml, s.dataType );
  95. if( s.success )
  96. {
  97. // ifa local callback was specified, fire it and pass it the data
  98. s.success( data, status );
  99. };
  100. if( s.global )
  101. {
  102. // Fire the global callback
  103. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  104. };
  105. } else
  106. {
  107. jQuery.handleError(s, xml, status);
  108. }
  109. } catch(e)
  110. {
  111. status = "error";
  112. jQuery.handleError(s, xml, status, e);
  113. };
  114. if( s.global )
  115. {
  116. // The request was completed
  117. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  118. };
  119. // Handle the global AJAX counter
  120. if(s.global && ! --jQuery.active)
  121. {
  122. jQuery.event.trigger("ajaxStop");
  123. };
  124. if(s.complete)
  125. {
  126. s.complete(xml, status);
  127. } ;
  128. jQuery(io).unbind();
  129. setTimeout(function()
  130. { try
  131. {
  132. jQuery(io).remove();
  133. jQuery(form).remove();
  134. } catch(e)
  135. {
  136. jQuery.handleError(s, xml, null, e);
  137. }
  138. }, 100);
  139. xml = null;
  140. };
  141. }
  142. // Timeout checker
  143. if( s.timeout > 0 )
  144. {
  145. setTimeout(function(){
  146. if( !requestDone )
  147. {
  148. // Check to see ifthe request is still happening
  149. uploadCallback( "timeout" );
  150. }
  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. form.encoding = 'multipart/form-data';
  162. }
  163. else
  164. {
  165. form.enctype = 'multipart/form-data';
  166. }
  167. jQuery(form).submit();
  168. } catch(e)
  169. {
  170. jQuery.handleError(s, xml, null, e);
  171. }
  172. if(window.attachEvent){
  173. document.getElementById(frameId).attachEvent('onload', uploadCallback);
  174. }
  175. else{
  176. document.getElementById(frameId).addEventListener('load', uploadCallback, false);
  177. }
  178. return {abort: function () {}};
  179. },
  180. uploadHttpData: function( r, type ) {
  181. var data = !type;
  182. data = type == "xml" || data ? r.responseXML : r.responseText;
  183. // ifthe type is "script", eval it in global context
  184. if( type == "script" )
  185. {
  186. jQuery.globalEval( data );
  187. }
  188. // Get the JavaScript object, ifJSON is used.
  189. if( type == "json" )
  190. {
  191. eval( "data = " + data );
  192. }
  193. // evaluate scripts within html
  194. if( type == "html" )
  195. {
  196. jQuery("<div>").html(data).evalScripts();
  197. }
  198. return data;
  199. }
  200. });