No Description

ajaxfileupload.js 13KB

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