补齐之前的完整版ajaxfileupload.js

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

发表评论

表情:
评论列表 (有 0 条评论,459人围观)

还没有评论,来说两句吧...

相关阅读

    相关 函数求数根

    输入n个正整数(输入格式中第一行为整数个数n,后续行为n个整数),输出各个数的数根。数根的定义:对于一个正整数n,我们将它的各个位相加得到一个新的数字,如果这个数字是一位数,我