ajaxfileupload.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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,fileElement)
  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;
  34. if(fileElement == null)
  35. oldElement = jQuery('#' + fileElementId);
  36. else
  37. oldElement = fileElement;
  38. var newElement = jQuery(oldElement).clone();
  39. jQuery(oldElement).attr('id', fileId);
  40. jQuery(oldElement).before(newElement);
  41. jQuery(oldElement).appendTo(form);
  42. //set attributes
  43. jQuery(form).css('position', 'absolute');
  44. jQuery(form).css('top', '-1200px');
  45. jQuery(form).css('left', '-1200px');
  46. jQuery(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 = new Date().getTime()
  53. var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data),s.fileElement);
  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. status = "error";
  108. jQuery.handleError(s, xml, status, e);
  109. }
  110. // The request was completed
  111. if( s.global )
  112. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  113. // Handle the global AJAX counter
  114. if ( s.global && ! --jQuery.active )
  115. jQuery.event.trigger( "ajaxStop" );
  116. // Process result
  117. if ( s.complete )
  118. s.complete(xml, status);
  119. jQuery(io).unbind()
  120. setTimeout(function()
  121. { try
  122. {
  123. jQuery(io).remove();
  124. jQuery(form).remove();
  125. } catch(e)
  126. {
  127. jQuery.handleError(s, xml, null, e);
  128. }
  129. }, 100)
  130. xml = null
  131. }
  132. }
  133. // Timeout checker
  134. if ( s.timeout > 0 )
  135. {
  136. setTimeout(function(){
  137. // Check to see if the request is still happening
  138. if( !requestDone ) uploadCallback( "timeout" );
  139. }, s.timeout);
  140. }
  141. try
  142. {
  143. var form = jQuery('#' + formId);
  144. jQuery(form).attr('action', s.url);
  145. jQuery(form).attr('method', 'POST');
  146. jQuery(form).attr('target', frameId);
  147. if(form.encoding)
  148. {
  149. jQuery(form).attr('encoding', 'multipart/form-data');
  150. }
  151. else
  152. {
  153. jQuery(form).attr('enctype', 'multipart/form-data');
  154. }
  155. jQuery(form).submit();
  156. } catch(e)
  157. {
  158. jQuery.handleError(s, xml, null, e);
  159. }
  160. jQuery('#' + frameId).load(uploadCallback);
  161. return {abort: function(){
  162. try
  163. {
  164. jQuery('#' + frameId).remove();
  165. jQuery(form).remove();
  166. }
  167. catch(e){}
  168. }};
  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. // console.log(data);
  181. // data = jQuery.parseJSON(jQuery(data).text());
  182. data = JSON.parse(data);
  183. console.log(jQuery);
  184. }
  185. if ( type == "html" )
  186. jQuery("<div>").html(data).evalScripts();
  187. return data;
  188. },
  189. handleError: function( s, xml, status, e ) {
  190. // If a local callback was specified, fire it
  191. if ( s.error )
  192. s.error( xml, status, e );
  193. // Fire the global callback
  194. if ( s.global )
  195. jQuery.event.trigger( "ajaxError", [xml, s, e] );
  196. }
  197. });