ajaxfileupload.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. jQuery.extend({
  2. createUploadIframe: function(id, uri)
  3. {
  4. //create frame
  5. var frameId = 'jUploadFrame' + id;
  6. if(window.ActiveXObject) {
  7. var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
  8. if(typeof uri== 'boolean'){
  9. io.src = 'javascript:false';
  10. }
  11. else if(typeof uri== 'string'){
  12. io.src = uri;
  13. }
  14. }
  15. else {
  16. var io = document.createElement('iframe');
  17. io.id = frameId;
  18. io.name = frameId;
  19. }
  20. io.style.position = 'absolute';
  21. io.style.top = '-1000px';
  22. io.style.left = '-1000px';
  23. document.body.appendChild(io);
  24. return io
  25. },
  26. createUploadForm: function(id, fileElementId,data)
  27. {
  28. //create form
  29. var formId = 'jUploadForm' + id;
  30. var fileId = 'jUploadFile' + id;
  31. var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  32. var oldElement = $('#' + fileElementId);
  33. var newElement = $(oldElement).clone();
  34. $(oldElement).attr('id', fileId);
  35. $(oldElement).before(newElement);
  36. $(oldElement).appendTo(form);
  37. if (data) {
  38. for (var i in data) {
  39. $('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  40. }
  41. }
  42. //set attributes
  43. $(form).css('position', 'absolute');
  44. $(form).css('top', '-1200px');
  45. $(form).css('left', '-1200px');
  46. $(form).appendTo('body');
  47. return form;
  48. },
  49. ajaxFileUpload: function(s) {
  50. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  51. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  52. var id = s.fileElementId;
  53. var form = jQuery.createUploadForm(id, s.fileElementId,s.data);
  54. var io = jQuery.createUploadIframe(id, s.secureuri);
  55. var frameId = 'jUploadFrame' + id;
  56. var formId = 'jUploadForm' + id;
  57. // Watch for a new set of requests
  58. if ( s.global && ! jQuery.active++ )
  59. {
  60. jQuery.event.trigger( "ajaxStart" );
  61. }
  62. var requestDone = false;
  63. // Create the request object
  64. var xml = {}
  65. if ( s.global )
  66. jQuery.event.trigger("ajaxSend", [xml, s]);
  67. // Wait for a response to come back
  68. var uploadCallback = function(isTimeout)
  69. {
  70. var io = document.getElementById(frameId);
  71. try
  72. {
  73. if(io.contentWindow)
  74. {
  75. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  76. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  77. }else if(io.contentDocument)
  78. {
  79. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  80. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  81. }
  82. }catch(e)
  83. {
  84. //jQuery.handleError(s, xml, null, e);
  85. }
  86. if ( xml || isTimeout == "timeout")
  87. {
  88. requestDone = true;
  89. var status;
  90. try {
  91. status = isTimeout != "timeout" ? "success" : "error";
  92. // Make sure that the request was successful or notmodified
  93. if ( status != "error" )
  94. {
  95. // process the data (runs the xml through httpData regardless of callback)
  96. var data = jQuery.uploadHttpData( xml, s.dataType );
  97. // If a local callback was specified, fire it and pass it the data
  98. if ( s.success )
  99. s.success( data, status );
  100. // Fire the global callback
  101. if( s.global )
  102. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  103. } else
  104. jQuery.handleError(s, xml, status);
  105. } catch(e)
  106. {
  107. //console.log(e);
  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. })