ajaxfileupload.js 7.0 KB

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