You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

174 regels
6.6 KiB

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