Sin descripción

ajaxfileupload.js 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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)
  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 = jQuery('#' + fileElementId);
  34. var newElement = jQuery(oldElement).clone();
  35. jQuery(oldElement).attr('id', fileId);
  36. jQuery(oldElement).before(newElement);
  37. jQuery(oldElement).appendTo(form);
  38. //set attributes
  39. jQuery(form).css('position', 'absolute');
  40. jQuery(form).css('top', '-1200px');
  41. jQuery(form).css('left', '-1200px');
  42. jQuery(form).appendTo('body');
  43. return form;
  44. },
  45. ajaxFileUpload: function(s) {
  46. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  47. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  48. var id = new Date().getTime()
  49. var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
  50. var io = jQuery.createUploadIframe(id, s.secureuri);
  51. var frameId = 'jUploadFrame' + id;
  52. var formId = 'jUploadForm' + id;
  53. // Watch for a new set of requests
  54. if ( s.global && ! jQuery.active++ )
  55. {
  56. jQuery.event.trigger( "ajaxStart" );
  57. }
  58. var requestDone = false;
  59. // Create the request object
  60. var xml = {}
  61. if ( s.global )
  62. jQuery.event.trigger("ajaxSend", [xml, s]);
  63. // Wait for a response to come back
  64. var uploadCallback = function(isTimeout)
  65. {
  66. var io = document.getElementById(frameId);
  67. try
  68. {
  69. if(io.contentWindow)
  70. {
  71. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  72. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  73. }else if(io.contentDocument)
  74. {
  75. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  76. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  77. }
  78. }catch(e)
  79. {
  80. jQuery.handleError(s, xml, null, e);
  81. }
  82. if ( xml || isTimeout == "timeout")
  83. {
  84. requestDone = true;
  85. var status;
  86. try {
  87. status = isTimeout != "timeout" ? "success" : "error";
  88. // Make sure that the request was successful or notmodified
  89. if ( status != "error" )
  90. {
  91. // process the data (runs the xml through httpData regardless of callback)
  92. var data = jQuery.uploadHttpData( xml, s.dataType );
  93. // If a local callback was specified, fire it and pass it the data
  94. if ( s.success )
  95. s.success( data, status );
  96. // Fire the global callback
  97. if( s.global )
  98. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  99. } else
  100. jQuery.handleError(s, xml, status);
  101. } catch(e)
  102. {
  103. status = "error";
  104. jQuery.handleError(s, xml, status, e);
  105. }
  106. // The request was completed
  107. if( s.global )
  108. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  109. // Handle the global AJAX counter
  110. if ( s.global && ! --jQuery.active )
  111. jQuery.event.trigger( "ajaxStop" );
  112. // Process result
  113. if ( s.complete )
  114. s.complete(xml, status);
  115. jQuery(io).unbind()
  116. setTimeout(function()
  117. { try
  118. {
  119. jQuery(io).remove();
  120. jQuery(form).remove();
  121. } catch(e)
  122. {
  123. jQuery.handleError(s, xml, null, e);
  124. }
  125. }, 100)
  126. xml = null
  127. }
  128. }
  129. // Timeout checker
  130. if ( s.timeout > 0 )
  131. {
  132. setTimeout(function(){
  133. // Check to see if the request is still happening
  134. if( !requestDone ) uploadCallback( "timeout" );
  135. }, s.timeout);
  136. }
  137. try
  138. {
  139. var form = jQuery('#' + formId);
  140. jQuery(form).attr('action', s.url);
  141. jQuery(form).attr('method', 'POST');
  142. jQuery(form).attr('target', frameId);
  143. if(form.encoding)
  144. {
  145. jQuery(form).attr('encoding', 'multipart/form-data');
  146. }
  147. else
  148. {
  149. jQuery(form).attr('enctype', 'multipart/form-data');
  150. }
  151. jQuery(form).submit();
  152. } catch(e)
  153. {
  154. jQuery.handleError(s, xml, null, e);
  155. }
  156. jQuery('#' + frameId).load(uploadCallback );
  157. return {abort: function () {}};
  158. },
  159. uploadHttpData: function( r, type ) {
  160. var data = !type;
  161. data = type == "xml" || data ? r.responseXML : r.responseText;
  162. // If the type is "script", eval it in global context
  163. if ( type == "script" )
  164. jQuery.globalEval( data );
  165. // Get the JavaScript object, if JSON is used.
  166. if ( type == "json" )
  167. eval( "data = " + data );
  168. // evaluate scripts within html
  169. if ( type == "html" )
  170. jQuery("<div>").html(data).evalScripts();
  171. return data;
  172. },handleError: function( s, xhr, status, e ) {
  173. // If a local callback was specified, fire it
  174. if ( s.error )
  175. s.error( xhr, status, e );
  176. // If we have some XML response text (e.g. from an AJAX call) then log it in the console
  177. else if(xhr.responseText)
  178. console.log(xhr.responseText);
  179. }
  180. })
  181. //jQuery.extend({
  182. //
  183. // createUploadIframe: function(id, uri)
  184. // {
  185. // //create frame
  186. // var frameId = 'jUploadFrame' + id;
  187. // var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
  188. // if(window.ActiveXObject)
  189. // {
  190. // if(typeof uri== 'boolean'){
  191. // iframeHtml += ' src="' + 'JavaScript:false' + '"';
  192. // }
  193. // else if(typeof uri== 'string'){
  194. // iframeHtml += ' src="' + uri + '"';
  195. // }
  196. // }
  197. // iframeHtml += ' />';
  198. // jQuery(iframeHtml).appendTo(document.body);
  199. // return jQuery('#' + frameId).get(0);
  200. // },
  201. // createUploadForm: function(id, fileElementId, data)
  202. // {
  203. // //create form
  204. // var formId = 'jUploadForm' + id;
  205. // var fileId = 'jUploadFile' + id;
  206. // var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  207. // if(data)
  208. // {
  209. // for(var i in data)
  210. // {
  211. // jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  212. // }
  213. // }
  214. // var oldElement = jQuery('#' + fileElementId);
  215. // var newElement = jQuery(oldElement).clone();
  216. // jQuery(oldElement).attr('id', fileId);
  217. // jQuery(oldElement).before(newElement);
  218. // jQuery(oldElement).appendTo(form);
  219. //
  220. //
  221. // //set attributes
  222. // jQuery(form).css('position', 'absolute');
  223. // jQuery(form).css('top', '-1200px');
  224. // jQuery(form).css('left', '-1200px');
  225. // jQuery(form).appendTo('body');
  226. // return form;
  227. // },
  228. // ajaxFileUpload: function(s) {
  229. // // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  230. // s = jQuery.extend({}, jQuery.ajaxSettings, s);
  231. // var id = new Date().getTime()
  232. // var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
  233. // var io = jQuery.createUploadIframe(id, s.secureuri);
  234. // var frameId = 'jUploadFrame' + id;
  235. // var formId = 'jUploadForm' + id;
  236. // // Watch for a new set of requests
  237. // if ( s.global && ! jQuery.active++ )
  238. // {
  239. // jQuery.event.trigger( "ajaxStart" );
  240. // }
  241. // var requestDone = false;
  242. // // Create the request object
  243. // var xml = {}
  244. // if ( s.global )
  245. // jQuery.event.trigger("ajaxSend", [xml, s]);
  246. // // Wait for a response to come back
  247. // var uploadCallback = function(isTimeout)
  248. // {
  249. // var io = document.getElementById(frameId);
  250. // try
  251. // {
  252. // if(io.contentWindow)
  253. // {
  254. // xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  255. // xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  256. //
  257. // }else if(io.contentDocument)
  258. // {
  259. // xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  260. // xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  261. // }
  262. // }catch(e)
  263. // {
  264. // jQuery.handleError(s, xml, null, e);
  265. // }
  266. // if ( xml || isTimeout == "timeout")
  267. // {
  268. // requestDone = true;
  269. // var status;
  270. // try {
  271. // status = isTimeout != "timeout" ? "success" : "error";
  272. // // Make sure that the request was successful or notmodified
  273. // if ( status != "error" )
  274. // {
  275. // // process the data (runs the xml through httpData regardless of callback)
  276. // var data = jQuery.uploadHttpData( xml, s.dataType );
  277. // // If a local callback was specified, fire it and pass it the data
  278. // if ( s.success )
  279. // s.success( data, status );
  280. //
  281. // // Fire the global callback
  282. // if( s.global )
  283. // jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  284. // } else
  285. // jQuery.handleError(s, xml, status);
  286. // } catch(e)
  287. // {
  288. // status = "error";
  289. // jQuery.handleError(s, xml, status, e);
  290. // }
  291. // // The request was completed
  292. // if( s.global )
  293. // jQuery.event.trigger( "ajaxComplete", [xml, s] );
  294. // // Handle the global AJAX counter
  295. // if ( s.global && ! --jQuery.active )
  296. // jQuery.event.trigger( "ajaxStop" );
  297. // // Process result
  298. // if ( s.complete )
  299. // s.complete(xml, status);
  300. // jQuery(io).unbind()
  301. // setTimeout(function()
  302. // { try
  303. // {
  304. // jQuery(io).remove();
  305. // jQuery(form).remove();
  306. //
  307. // } catch(e)
  308. // {
  309. // jQuery.handleError(s, xml, null, e);
  310. // }
  311. // }, 100)
  312. // xml = null
  313. // }
  314. // }
  315. // // Timeout checker
  316. // if ( s.timeout > 0 )
  317. // {
  318. // setTimeout(function(){
  319. // // Check to see if the request is still happening
  320. // if( !requestDone ) uploadCallback( "timeout" );
  321. // }, s.timeout);
  322. // }
  323. // try
  324. // {
  325. // var form = jQuery('#' + formId);
  326. // jQuery(form).attr('action', s.url);
  327. // jQuery(form).attr('method', 'POST');
  328. // jQuery(form).attr('target', frameId);
  329. // if(form.encoding)
  330. // {
  331. // jQuery(form).attr('encoding', 'multipart/form-data');
  332. // }
  333. // else
  334. // {
  335. // jQuery(form).attr('enctype', 'multipart/form-data');
  336. // }
  337. // jQuery(form).submit();
  338. // } catch(e)
  339. // {
  340. // jQuery.handleError(s, xml, null, e);
  341. // }
  342. //
  343. // jQuery('#' + frameId).load(uploadCallback );
  344. // return {abort: function () {}};
  345. // },
  346. // uploadHttpData: function( r, type ) {
  347. // var data = !type;
  348. // data = type == "xml" || data ? r.responseXML : r.responseText;
  349. // // If the type is "script", eval it in global context
  350. // if ( type == "script" )
  351. // jQuery.globalEval( data );
  352. // // Get the JavaScript object, if JSON is used.
  353. // if ( type == "json" )
  354. // eval( "data = " + data );
  355. // // evaluate scripts within html
  356. // if ( type == "html" )
  357. // jQuery("<div>").html(data).evalScripts();
  358. // return data;
  359. // }
  360. //})