ajaxfileupload.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. jQuery.extend({
  2. createUploadIframe: function(id, uri) {
  3. var frameId = "jUploadFrame" + id;
  4. var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
  5. if (window.ActiveXObject) {
  6. if (typeof uri == "boolean") {
  7. iframeHtml += ' src="' + "javascript:false" + '"'
  8. } else {
  9. if (typeof uri == "string") {
  10. iframeHtml += ' src="' + uri + '"'
  11. }
  12. }
  13. }
  14. iframeHtml += " />";
  15. jQuery(iframeHtml).appendTo(document.body);
  16. return jQuery("#" + frameId).get(0)
  17. },
  18. createUploadForm: function(id, fileElementId, data) {
  19. var formId = "jUploadForm" + id;
  20. var fileId = "jUploadFile" + id;
  21. var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  22. if (data) {
  23. for (var i in data) {
  24. jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form)
  25. }
  26. }
  27. var oldElement = jQuery("#" + fileElementId);
  28. var newElement = jQuery(oldElement).clone();
  29. jQuery(oldElement).attr("id", fileId);
  30. jQuery(oldElement).before(newElement);
  31. jQuery(oldElement).appendTo(form);
  32. jQuery(form).css("position", "absolute");
  33. jQuery(form).css("top", "-1200px");
  34. jQuery(form).css("left", "-1200px");
  35. jQuery(form).appendTo("body");
  36. return form
  37. },
  38. ajaxFileUpload: function(s) {
  39. s = jQuery.extend({},
  40. jQuery.ajaxSettings, s);
  41. var id = new Date().getTime();
  42. var form = jQuery.createUploadForm(id, s.fileElementId, (typeof(s.data) == "undefined" ? false: s.data));
  43. var io = jQuery.createUploadIframe(id, s.secureuri);
  44. var frameId = "jUploadFrame" + id;
  45. var formId = "jUploadForm" + id;
  46. if (s.global && !jQuery.active++) {
  47. jQuery.event.trigger("ajaxStart")
  48. }
  49. var requestDone = false;
  50. var xml = {};
  51. if (s.global) {
  52. jQuery.event.trigger("ajaxSend", [xml, s])
  53. }
  54. var uploadCallback = function(isTimeout) {
  55. var io = document.getElementById(frameId);
  56. try {
  57. if (io.contentWindow) {
  58. xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML: null;
  59. xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument: io.contentWindow.document
  60. } else {
  61. if (io.contentDocument) {
  62. xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML: null;
  63. xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument: io.contentDocument.document
  64. }
  65. }
  66. } catch(e) {
  67. jQuery.handleError(s, xml, null, e)
  68. }
  69. if (xml || isTimeout == "timeout") {
  70. requestDone = true;
  71. var status;
  72. try {
  73. status = isTimeout != "timeout" ? "success": "error";
  74. if (status != "error") {
  75. var data = jQuery.uploadHttpData(xml, s.dataType);
  76. if (s.success) {
  77. s.success(data, status)
  78. }
  79. if (s.global) {
  80. jQuery.event.trigger("ajaxSuccess", [xml, s])
  81. }
  82. } else {
  83. jQuery.handleError(s, xml, status)
  84. }
  85. } catch(e) {
  86. status = "error";
  87. jQuery.handleError(s, xml, status, e)
  88. }
  89. if (s.global) {
  90. jQuery.event.trigger("ajaxComplete", [xml, s])
  91. }
  92. if (s.global && !--jQuery.active) {
  93. jQuery.event.trigger("ajaxStop")
  94. }
  95. if (s.complete) {
  96. s.complete(xml, status)
  97. }
  98. jQuery(io).unbind();
  99. setTimeout(function() {
  100. try {
  101. jQuery(io).remove();
  102. jQuery(form).remove()
  103. } catch(e) {
  104. jQuery.handleError(s, xml, null, e)
  105. }
  106. },
  107. 100);
  108. xml = null
  109. }
  110. };
  111. if (s.timeout > 0) {
  112. setTimeout(function() {
  113. if (!requestDone) {
  114. uploadCallback("timeout")
  115. }
  116. },
  117. s.timeout)
  118. }
  119. try {
  120. var form = jQuery("#" + formId);
  121. jQuery(form).attr("action", s.url);
  122. jQuery(form).attr("method", "POST");
  123. jQuery(form).attr("target", frameId);
  124. if (form.encoding) {
  125. jQuery(form).attr("encoding", "multipart/form-data")
  126. } else {
  127. jQuery(form).attr("enctype", "multipart/form-data")
  128. }
  129. jQuery(form).submit()
  130. } catch(e) {
  131. jQuery.handleError(s, xml, null, e)
  132. }
  133. jQuery("#" + frameId).load(uploadCallback);
  134. return {
  135. abort: function() {}
  136. }
  137. },
  138. uploadHttpData: function(r, type) {
  139. var data = !type;
  140. data = type == "xml" || data ? r.responseXML: r.responseText;
  141. if (type == "script") {
  142. jQuery.globalEval(data)
  143. }
  144. if (type == "json") {
  145. ////////////以下为新增代码///////////////
  146. data = r.responseText;
  147. var start = data.indexOf(">");
  148. if(start != -1) {
  149. var end = data.indexOf("<", start + 1);
  150. if(end != -1) {
  151. data = data.substring(start + 1, end);
  152. }
  153. }
  154. ///////////以上为新增代码///////////////
  155. eval("data = " + data)
  156. }
  157. if (type == "html") {
  158. jQuery("<div>").html(data).evalScripts()
  159. }
  160. return data
  161. },
  162. handleError: function(s, xhr, status, e) {
  163. if (s.error) {
  164. s.error.call(s.context || s, xhr, status, e)
  165. }
  166. if (s.global) { (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e])
  167. }
  168. },
  169. });