ajaxfileupload.js 8.4 KB

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