portal html css js resource

ajaxfileupload.js 8.4KB

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