upload.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. jQuery.yihuUpload = {
  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. var _data = data.split('&');
  28. for(var i in _data){
  29. if (_data[i]) {
  30. var _d = _data[i].split('=');
  31. if(_d){
  32. jQuery('<input type="hidden" name="' + _d[0] + '" value="' + _d[1] + '" />').appendTo(form);
  33. }
  34. }
  35. }
  36. }
  37. for (var i = 0; i < fileElementId.length; i ++) {
  38. var oldElement = jQuery('#' + fileElementId[i]);
  39. var newElement = jQuery(oldElement).clone();
  40. jQuery(oldElement).attr('id', fileElementId[i]);
  41. jQuery(oldElement).attr('name', fileElementId[i].name);
  42. jQuery(oldElement).before(newElement);
  43. jQuery(oldElement).appendTo(form);
  44. }
  45. //set attributes
  46. jQuery(form).css('position', 'absolute');
  47. jQuery(form).css('top', '-1200px');
  48. jQuery(form).css('left', '-1200px');
  49. jQuery(form).appendTo('body');
  50. return form;
  51. } ,
  52. handleError : function( s, xhr, status, e ) {
  53. // If a local callback was specified, fire it
  54. if ( s.error ) {
  55. s.error.call( s.context || s, xhr, status, e );
  56. }
  57. // Fire the global callback
  58. if ( s.global ) {
  59. (s.context ? jQuery(s.context) : jQuery.event).trigger( "ajaxError", [xhr, s, e] );
  60. }
  61. } ,
  62. ajaxFileUpload: function(s) {
  63. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  64. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  65. var id = new Date().getTime()
  66. var form = $.yihuUpload.createUploadForm(id, s.fileElementId,s.data);
  67. var io = $.yihuUpload.createUploadIframe(id, s.secureuri);
  68. var frameId = 'jUploadFrame' + id;
  69. var formId = 'jUploadForm' + id;
  70. // Watch for a new set of requests
  71. if ( s.global && ! jQuery.active++ )
  72. {
  73. jQuery.event.trigger( "ajaxStart" );
  74. }
  75. var requestDone = false;
  76. // Create the request object
  77. var xml = {}
  78. if ( s.global )
  79. jQuery.event.trigger("ajaxSend", [xml, s]);
  80. // Wait for a response to come back
  81. var uploadCallback = function(isTimeout)
  82. {
  83. var io = document.getElementById(frameId);
  84. try
  85. {
  86. if(io.contentWindow)
  87. {
  88. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  89. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  90. }else if(io.contentDocument)
  91. {
  92. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  93. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  94. }
  95. }catch(e)
  96. {
  97. $.yihuUpload.handleError(s, xml, null, e);
  98. }
  99. if ( xml || isTimeout == "timeout")
  100. {
  101. requestDone = true;
  102. var status;
  103. try {
  104. status = isTimeout != "timeout" ? "success" : "error";
  105. // Make sure that the request was successful or notmodified
  106. if ( status != "error" )
  107. {
  108. // process the data (runs the xml through httpData regardless of callback)
  109. var data = $.yihuUpload.uploadHttpData( xml, s.dataType );
  110. // If a local callback was specified, fire it and pass it the data
  111. if ( s.success )
  112. s.success( data, status );
  113. // Fire the global callback
  114. if( s.global )
  115. jQuery.event.trigger( "ajaxSuccess", [xml, s] );
  116. } else
  117. $.yihuUpload.handleError(s, xml, status);
  118. } catch(e)
  119. {
  120. status = "error";
  121. $.yihuUpload.handleError(s, xml, status, e);
  122. }
  123. // The request was completed
  124. if( s.global )
  125. jQuery.event.trigger( "ajaxComplete", [xml, s] );
  126. // Handle the global AJAX counter
  127. if ( s.global && ! --jQuery.active )
  128. jQuery.event.trigger( "ajaxStop" );
  129. // Process result
  130. if ( s.complete )
  131. s.complete(xml, status);
  132. jQuery(io).unbind()
  133. setTimeout(function()
  134. { try
  135. {
  136. jQuery(io).remove();
  137. jQuery(form).remove();
  138. } catch(e)
  139. {
  140. $.yihuUpload.handleError(s, xml, null, e);
  141. }
  142. }, 100)
  143. xml = null
  144. }
  145. }
  146. // Timeout checker
  147. if ( s.timeout > 0 )
  148. {
  149. setTimeout(function(){
  150. // Check to see if the request is still happening
  151. if( !requestDone ) uploadCallback( "timeout" );
  152. }, s.timeout);
  153. }
  154. try
  155. {
  156. var form = jQuery('#' + formId);
  157. jQuery(form).attr('action', s.url);
  158. jQuery(form).attr('method', 'POST');
  159. jQuery(form).attr('target', frameId);
  160. if(form.encoding)
  161. {
  162. jQuery(form).attr('encoding', 'multipart/form-data');
  163. }
  164. else
  165. {
  166. jQuery(form).attr('enctype', 'multipart/form-data');
  167. }
  168. jQuery(form).submit();
  169. } catch(e)
  170. {
  171. $.yihuUpload.handleError(s, xml, null, e);
  172. }
  173. $('#' + frameId).load(uploadCallback );
  174. return {abort: function () {}};
  175. },
  176. uploadHttpData: function( r, type ) {
  177. var data = !type;
  178. data = type == "xml" || data ? r.responseXML : r.responseText;
  179. // If the type is "script", eval it in global context
  180. if ( type == "script" )
  181. jQuery.globalEval( data );
  182. // Get the JavaScript object, if JSON is used.
  183. if ( type == "json" )
  184. eval( "data = " + data );
  185. // evaluate scripts within html
  186. if ( type == "html" )
  187. jQuery("<div>").html(data).evalScripts();
  188. return data;
  189. },
  190. post:function(bizAction,param,successFn)
  191. {
  192. param.bizAction=bizAction;
  193. $.ajax({
  194. url :this.url,
  195. data:param,
  196. type : "POST",
  197. dataType : 'json',
  198. success : successFn
  199. });
  200. }
  201. };