ajaxfileupload.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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)
  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. //set attributes
  38. $(form).css('position', 'absolute');
  39. $(form).css('top', '-1200px');
  40. $(form).css('left', '-1200px');
  41. $(form).appendTo('body');
  42. return form;
  43. },
  44. addOtherRequestsToForm: function(form,data)
  45. {
  46. // add extra parameter
  47. var originalElement = $('<input type="hidden" name="" value="">');
  48. for (var key in data) {
  49. name = key;
  50. value = data[key];
  51. var cloneElement = originalElement.clone();
  52. cloneElement.attr({'name':name,'value':value});
  53. $(cloneElement).appendTo(form);
  54. }
  55. return form;
  56. },
  57. ajaxFileUpload: function(s) {
  58. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  59. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  60. var id = new Date().getTime()
  61. var form = jQuery.createUploadForm(id, s.fileElementId);
  62. if ( s.data ) form = jQuery.addOtherRequestsToForm(form,s.data);
  63. var io = jQuery.createUploadIframe(id, s.secureuri);
  64. var frameId = 'jUploadFrame' + id;
  65. var formId = 'jUploadForm' + id;
  66. // Watch for a new set of requests
  67. if ( s.global && ! jQuery.active++ )
  68. {
  69. jQuery.event.trigger( "ajaxStart" );
  70. }
  71. var requestDone = false;
  72. // Create the request object
  73. var xml = {}
  74. if ( s.global )
  75. jQuery.event.trigger("ajaxSend", [xml, s]);
  76. // Wait for a response to come back
  77. var uploadCallback = function(isTimeout)
  78. {
  79. var io = document.getElementById(frameId);
  80. try
  81. {
  82. if(io.contentWindow)
  83. {
  84. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  85. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  86. }else if(io.contentDocument)
  87. {
  88. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  89. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  90. }
  91. }catch(e)
  92. {
  93. jQuery.handleError(s, xml, null, e);
  94. }
  95. if ( xml || isTimeout == "timeout")
  96. {
  97. requestDone = true;
  98. var status;
  99. try {
  100. status = isTimeout != "timeout" ? "success" : "error";
  101. // Make sure that the request was successful or notmodified
  102. if ( status != "error" )
  103. {
  104. // process the data (runs the xml through httpData regardless of callback)
  105. var data = jQuery.uploadHttpData( xml, s.dataType );
  106. // If a local callback was specified, fire it and pass it the data
  107. if ( s.success )
  108. s.success( data, status );
  109. // Fire the global callback
  110. if( s.global )
  111. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  112. } else
  113. jQuery.handleError(s, xml, status);
  114. } catch(e)
  115. {
  116. status = "error";
  117. jQuery.handleError(s, xml, status, e);
  118. }
  119. // The request was completed
  120. if( s.global )
  121. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  122. // Handle the global AJAX counter
  123. if ( s.global && ! --jQuery.active )
  124. jQuery.event.trigger( "ajaxStop" );
  125. // Process result
  126. if ( s.complete )
  127. s.complete(xml, status);
  128. jQuery(io).unbind()
  129. setTimeout(function()
  130. { try
  131. {
  132. $(io).remove();
  133. $(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. // Check to see if the request is still happening
  147. if( !requestDone ) uploadCallback( "timeout" );
  148. }, s.timeout);
  149. }
  150. try
  151. {
  152. // var io = $('#' + frameId);
  153. var form = $('#' + formId);
  154. $(form).attr('action', s.url);
  155. $(form).attr('method', 'POST');
  156. $(form).attr('target', frameId);
  157. if(form.encoding)
  158. {
  159. form.encoding = 'multipart/form-data';
  160. }
  161. else
  162. {
  163. form.enctype = 'multipart/form-data';
  164. }
  165. $(form).submit();
  166. } catch(e)
  167. {
  168. jQuery.handleError(s, xml, null, e);
  169. }
  170. if(window.attachEvent){
  171. document.getElementById(frameId).attachEvent('onload', uploadCallback);
  172. }
  173. else{
  174. document.getElementById(frameId).addEventListener('load', uploadCallback, false);
  175. }
  176. return {abort: function () {}};
  177. },
  178. uploadHttpData: function( r, type ) {
  179. var data = !type;
  180. data = type == "xml" || data ? r.responseXML : r.responseText;
  181. // If the type is "script", eval it in global context
  182. if ( type == "script" )
  183. jQuery.globalEval( data );
  184. // Get the JavaScript object, if JSON is used.
  185. if ( type == "json" )
  186. {
  187. // If you add mimetype in your response,
  188. // you have to delete the '<pre></pre>' tag.
  189. // The pre tag in Chrome has attribute, so have to use regex to remove
  190. var data = r.responseText;
  191. var rx = new RegExp("<pre.*?>(.*?)</pre>","i");
  192. var am = rx.exec(data);
  193. //this is the desired data extracted
  194. var data = (am) ? am[1] : ""; //the only submatch or empty
  195. eval( "data = " + data );
  196. }
  197. // evaluate scripts within html
  198. if ( type == "html" )
  199. jQuery("<div>").html(data).evalScripts();
  200. //alert($('param', data).each(function(){alert($(this).attr('value'));}));
  201. return data;
  202. }
  203. })