ajaxfileupload.js 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. 
  2. jQuery.extend({
  3. handleError: function (s, xhr, status, e) {
  4. // If a local callback was specified, fire it
  5. if (s.error) {
  6. s.error.call(s.context || s, xhr, status, e);
  7. }
  8. // Fire the global callback
  9. if (s.global) {
  10. (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
  11. }
  12. },
  13. createUploadIframe: function (id, uri) {
  14. //create frame
  15. var frameId = 'jUploadFrame' + id;
  16. var iframeHtml = '<iframe id="' + frameId + '" name="' + frameId + '" style="position:absolute; top:-9999px; left:-9999px"';
  17. if (window.ActiveXObject) {
  18. if (typeof uri == 'boolean') {
  19. iframeHtml += ' src="' + 'javascript:false' + '"';
  20. }
  21. else if (typeof uri == 'string') {
  22. iframeHtml += ' src="' + uri + '"';
  23. }
  24. }
  25. iframeHtml += ' />';
  26. jQuery(iframeHtml).appendTo(document.body);
  27. return jQuery('#' + frameId).get(0);
  28. },
  29. createUploadForm: function (id, fileElementId, data) {
  30. //create form
  31. var formId = 'jUploadForm' + id;
  32. var fileId = 'jUploadFile' + id;
  33. var form = jQuery('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
  34. if (data) {
  35. for (var i in data) {
  36. //jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  37. jQuery('<input type="hidden" name="' + i + '" />').appendTo(form).val(data[i]);
  38. }
  39. }
  40. var oldElement = typeof fileElementId == "string" ? jQuery('#' + fileElementId) : fileElementId;
  41. var newElement = jQuery(oldElement).clone();
  42. jQuery(oldElement).attr('id', fileId);
  43. jQuery(oldElement).before(newElement);
  44. jQuery(oldElement).appendTo(form);
  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. ajaxFileUpload: function (s) {
  53. // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
  54. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  55. var id = new Date().getTime()
  56. var form = jQuery.createUploadForm(id, s.fileElementId, (typeof (s.data) == 'undefined' ? false : s.data));
  57. var io = jQuery.createUploadIframe(id, s.secureuri);
  58. var frameId = 'jUploadFrame' + id;
  59. var formId = 'jUploadForm' + id;
  60. // Watch for a new set of requests
  61. if (s.global && !jQuery.active++) {
  62. jQuery.event.trigger("ajaxStart");
  63. }
  64. var requestDone = false;
  65. // Create the request object
  66. var xml = {}
  67. if (s.global)
  68. jQuery.event.trigger("ajaxSend", [xml, s]);
  69. // Wait for a response to come back
  70. var uploadCallback = function (isTimeout) {
  71. var io = document.getElementById(frameId);
  72. try {
  73. if (io.contentWindow) {
  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. } else if (io.contentDocument) {
  77. xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null;
  78. xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document;
  79. }
  80. } catch (e) {
  81. jQuery.handleError(s, xml, null, e);
  82. }
  83. if (xml || isTimeout == "timeout") {
  84. requestDone = true;
  85. var status;
  86. try {
  87. status = isTimeout != "timeout" ? "success" : "error";
  88. // Make sure that the request was successful or notmodified
  89. if (status != "error") {
  90. // process the data (runs the xml through httpData regardless of callback)
  91. var data = jQuery.uploadHttpData(xml, s.dataType);
  92. // If a local callback was specified, fire it and pass it the data
  93. if (s.success)
  94. s.success(data, status);
  95. // Fire the global callback
  96. if (s.global)
  97. jQuery.event.trigger("ajaxSuccess", [xml, s]);
  98. } else
  99. jQuery.handleError(s, xml, status);
  100. } catch (e) {
  101. status = "error";
  102. jQuery.handleError(s, xml, status, e);
  103. }
  104. // The request was completed
  105. if (s.global)
  106. jQuery.event.trigger("ajaxComplete", [xml, s]);
  107. // Handle the global AJAX counter
  108. if (s.global && ! --jQuery.active)
  109. jQuery.event.trigger("ajaxStop");
  110. // Process result
  111. if (s.complete)
  112. s.complete(xml, status);
  113. jQuery(io).unbind()
  114. setTimeout(function () {
  115. try {
  116. jQuery(io).remove();
  117. jQuery(form).remove();
  118. } catch (e) {
  119. jQuery.handleError(s, xml, null, e);
  120. }
  121. }, 100)
  122. xml = null
  123. }
  124. }
  125. // Timeout checker
  126. if (s.timeout > 0) {
  127. setTimeout(function () {
  128. // Check to see if the request is still happening
  129. if (!requestDone) uploadCallback("timeout");
  130. }, s.timeout);
  131. }
  132. try {
  133. var form = jQuery('#' + formId);
  134. jQuery(form).attr('action', s.url);
  135. jQuery(form).attr('method', 'POST');
  136. jQuery(form).attr('target', frameId);
  137. if (form.encoding) {
  138. jQuery(form).attr('encoding', 'multipart/form-data');
  139. }
  140. else {
  141. jQuery(form).attr('enctype', 'multipart/form-data');
  142. }
  143. jQuery(form).submit();
  144. } catch (e) {
  145. jQuery.handleError(s, xml, null, e);
  146. }
  147. jQuery('#' + frameId).load(s.url,uploadCallback);
  148. return { abort: function () { } };
  149. },
  150. uploadHttpData: function (r, type) {
  151. var data = !type;
  152. data = type == "xml" || data ? r.responseXML : r.responseText;
  153. // If the type is "script", eval it in global context
  154. if (type == "script")
  155. jQuery.globalEval(data);
  156. // Get the JavaScript object, if JSON is used.
  157. if (type == "json")
  158. eval("data=" + jQuery.parseJSON(data).text());
  159. // evaluate scripts within html
  160. if (type == "html")
  161. jQuery("<div>").html(data).evalScripts();
  162. return data;
  163. }
  164. })