ajaxfileupload.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. jQuery.extend({
  2. createuploadiframe: function(id, url) {
  3. var iframeid = 'uploadiframe' + id;
  4. var iframe = '<iframe id="' + iframeid + '" name="' + iframeid + '"';
  5. if(window.ActiveXObject) {
  6. if(typeof url == 'boolean') {
  7. iframe += ' src="' + 'javascript:false' + '"';
  8. } else if(typeof url == 'string') {
  9. iframe += ' src="' + url + '"';
  10. }
  11. }
  12. iframe += ' />';
  13. jQuery(iframe).css({'position':'absolute', 'top':'-1200px', 'left':'-1200px'}).appendTo(document.body);
  14. return jQuery('#' + iframeid).get(0);
  15. },
  16. createuploadform: function(id, fileobjid, data) {
  17. var formid = 'uploadform' + id;
  18. var fileid = 'uploadfile' + id;
  19. var form = jQuery('<form method="post" name="' + formid + '" id="' + formid + '" enctype="multipart/form-data"></form>');
  20. if(data) {
  21. for(var i in data) {
  22. jQuery('<input type="hidden" name="' + i + '" value="' + data[i] + '" />').appendTo(form);
  23. }
  24. }
  25. var oldobj = jQuery('#' + fileobjid);
  26. var newobj = jQuery(oldobj).clone();
  27. jQuery(oldobj).attr('id', fileid).before(newobj).appendTo(form);
  28. jQuery(form).css({'position':'absolute', 'top':'-1200px', 'left':'-1200px'}).appendTo(document.body);
  29. return form;
  30. },
  31. ajaxfileupload: function(s) {
  32. s = jQuery.extend({}, jQuery.ajaxSettings, s);
  33. var id = new Date().getTime();
  34. var form = jQuery.createuploadform(id, s.fileElementId, (typeof(s.data)=='undefined'?false:s.data));
  35. var io = jQuery.createuploadiframe(id, s.secureuri);
  36. var iframeid = 'uploadiframe' + id;
  37. var formid = 'uploadform' + id;
  38. if(s.global && ! jQuery.active++) {
  39. jQuery.event.trigger("ajaxStart");
  40. }
  41. var requestDone = false;
  42. var xml = {};
  43. if(s.global) {
  44. jQuery.event.trigger("ajaxSend", [xml, s]);
  45. }
  46. var uploadcallback = function(istimeout) {
  47. var io = document.getElementById(iframeid);
  48. try {
  49. if(io.contentWindow) {
  50. xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
  51. xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
  52. } else if(io.contentDocument) {
  53. xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
  54. xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
  55. }
  56. } catch(e) {
  57. jQuery.handleerror(s, xml, null, e);
  58. }
  59. if(xml||istimeout == 'timeout') {
  60. requestdone = true;
  61. var status;
  62. try {
  63. status = istimeout != 'timeout' ? 'success' : 'error';
  64. if(status != 'error') {
  65. var data = jQuery.uploadhttpdata(xml, s.dataType);
  66. if(s.success) {
  67. s.success( data, status );
  68. }
  69. if(s.global) {
  70. jQuery.event.trigger("ajaxSuccess", [xml, s]);
  71. }
  72. } else {
  73. jQuery.handleerror(s, xml, status);
  74. }
  75. } catch(e) {
  76. status = 'error';
  77. jQuery.handleerror(s, xml, status, e);
  78. }
  79. if(s.global) {
  80. jQuery.event.trigger("ajaxComplete", [xml, s]);
  81. }
  82. if(s.global && ! --jQuery.active) {
  83. jQuery.event.trigger("ajaxStop");
  84. }
  85. if (s.complete) {
  86. s.complete(xml, status);
  87. }
  88. jQuery(io).off();
  89. setTimeout(function() {
  90. try {
  91. jQuery(io).remove();
  92. jQuery(form).remove();
  93. } catch(e) {
  94. jQuery.handleerror(s, xml, null, e);
  95. }
  96. }, 100);
  97. xml = null;
  98. }
  99. }
  100. if(s.timeout > 0) {
  101. setTimeout(function() {
  102. if(!requestdone) {
  103. uploadcallback('timeout');
  104. }
  105. }, s.timeout);
  106. }
  107. try {
  108. var form = jQuery('#' + formid);
  109. jQuery(form).attr('action', s.url).attr('method', 'post').attr('target', iframeid);
  110. if(form.encoding) {
  111. jQuery(form).attr('encoding', 'multipart/form-data');
  112. } else {
  113. jQuery(form).attr('enctype', 'multipart/form-data');
  114. }
  115. jQuery(form).submit();
  116. } catch(e) {
  117. jQuery.handleerror(s, xml, null, e);
  118. }
  119. jQuery('#' + iframeid).load(uploadcallback);
  120. return {abort: function () {}};
  121. },
  122. uploadhttpdata: function(r, type) {
  123. var data = !type;
  124. data = type == 'xml' || data ? r.responseXML : r.responseText;
  125. if(type == 'script') {
  126. jQuery.globalEval(data);
  127. }
  128. if(type == "json") {
  129. eval("data = " + data);
  130. }
  131. if(type == "html") {
  132. jQuery("<div>").html(data);
  133. }
  134. return data;
  135. },
  136. handleerror: function(s, xhr, status, e) {
  137. if(s.error) {
  138. s.error.call(s.context || s, xhr, status, e);
  139. }
  140. if(s.global) {
  141. (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
  142. }
  143. }
  144. });