fileupLoad.js 5.7 KB

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