2 createUploadIframe: function (id, uri) {
\r
4 var frameId = 'jUploadFrame' + id;
\r
5 if (window.ActiveXObject) {
\r
6 var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
\r
7 if (typeof uri == 'boolean') {
\r
8 io.src = 'javascript:false';
\r
10 else if (typeof uri == 'string') {
\r
15 var io = document.createElement('iframe');
\r
19 io.style.position = 'absolute';
\r
20 io.style.top = '-1000px';
\r
21 io.style.left = '-1000px';
\r
22 document.body.appendChild(io);
\r
25 createUploadForm: function (id, fileElementId) {
\r
27 var formId = 'jUploadForm' + id;
\r
28 var fileId = 'jUploadFile' + id;
\r
29 var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
\r
30 var oldElement = $('#' + fileElementId);
\r
31 var newElement = $(oldElement).clone();
\r
32 $(oldElement).attr('id', fileId);
\r
33 $(oldElement).before(newElement);
\r
34 $(oldElement).appendTo(form);
\r
36 $(form).css('position', 'absolute');
\r
37 $(form).css('top', '-1200px');
\r
38 $(form).css('left', '-1200px');
\r
39 $(form).appendTo('body');
\r
42 ajaxFileUpload: function (s) {
\r
43 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
\r
44 s = jQuery.extend({}, jQuery.ajaxSettings, s);
\r
45 var id = new Date().getTime()
\r
46 var form = jQuery.createUploadForm(id, s.fileElementId);
\r
47 var io = jQuery.createUploadIframe(id, s.secureuri);
\r
48 var frameId = 'jUploadFrame' + id;
\r
49 var formId = 'jUploadForm' + id;
\r
50 // Watch for a new set of requests
\r
51 if (s.global && !jQuery.active++) {
\r
52 jQuery.event.trigger("ajaxStart");
\r
54 var requestDone = false;
\r
55 // Create the request object
\r
57 if (s.global) jQuery.event.trigger("ajaxSend", [xml, s]);
\r
58 // Wait for a response to come back
\r
59 var uploadCallback = function (isTimeout) {
\r
60 var io = document.getElementById(frameId);
\r
62 if (io.contentWindow) {
\r
63 xml.responseText = io.contentWindow.document.body ? io.contentWindow.document.body.innerHTML : null;
\r
64 xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document;
\r
65 } else if (io.contentDocument) {
\r
66 xml.responseText = io.contentDocument.document.body ? io.contentDocument.document.body.innerHTML : null;
\r
67 xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document;
\r
70 jQuery.handleError(s, xml, null, e);
\r
72 if (xml || isTimeout == "timeout") {
\r
76 status = isTimeout != "timeout" ? "success" : "error";
\r
77 // Make sure that the request was successful or notmodified
\r
78 if (status != "error") {
\r
79 // process the data (runs the xml through httpData regardless of callback)
\r
80 var data = jQuery.uploadHttpData(xml, s.dataType);
\r
81 // If a local callback was specified, fire it and pass it the data
\r
82 if (s.success) s.success(data, status);
\r
83 // Fire the global callback
\r
84 if (s.global) jQuery.event.trigger("ajaxSuccess", [xml, s]);
\r
85 } else jQuery.handleError(s, xml, status);
\r
88 jQuery.handleError(s, xml, status, e);
\r
90 // The request was completed
\r
91 if (s.global) jQuery.event.trigger("ajaxComplete", [xml, s]);
\r
92 // Handle the global AJAX counter
\r
93 if (s.global && !--jQuery.active) jQuery.event.trigger("ajaxStop");
\r
95 if (s.complete) s.complete(xml, status);
\r
97 setTimeout(function () {
\r
102 jQuery.handleError(s, xml, null, e);
\r
109 if (s.timeout > 0) {
\r
110 setTimeout(function () {
\r
111 // Check to see if the request is still happening
\r
112 if (!requestDone) uploadCallback("timeout");
\r
116 // var io = $('#' + frameId);
\r
117 var form = $('#' + formId);
\r
118 $(form).attr('action', s.url);
\r
119 $(form).attr('method', 'POST');
\r
120 $(form).attr('target', frameId);
\r
121 if (form.encoding) {
\r
122 form.encoding = 'multipart/form-data';
\r
125 form.enctype = 'multipart/form-data';
\r
129 jQuery.handleError(s, xml, null, e);
\r
131 if (window.attachEvent) {
\r
132 document.getElementById(frameId).attachEvent('onload', uploadCallback);
\r
135 document.getElementById(frameId).addEventListener('load', uploadCallback, false);
\r
138 abort: function () {}
\r
141 uploadHttpData: function (r, type) {
\r
143 data = type == "xml" || data ? r.responseXML : r.responseText;
\r
144 // If the type is "script", eval it in global context
\r
145 if (type == "script") jQuery.globalEval(data);
\r
146 // Get the JavaScript object, if JSON is used.
\r
147 if (type == "json") eval("data = " + data);
\r
148 // evaluate scripts within html
\r
149 if (type == "html") jQuery("<div>").html(data).evalScripts();
\r