ajaxfileupload.js 8.2 KB

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