$j.extend({ createUploadIframe: function (id, uri) { //create frame var frameId = 'jUploadFrame' + id; var iframeHtml = ''; $j(iframeHtml).appendTo(document.body); return $j('#' + frameId).get(0); }, createUploadForm: function (id, fileElementId, data) { //create form var formId = 'jUploadForm' + id; var fileId = 'jUploadFile' + id; var form = $j('
'); if (data) { for (var i in data) { $j('').appendTo(form); } } var oldElement = $j('#' + fileElementId); var newElement = $j(oldElement).clone(); $j(oldElement).attr('id', fileId); $j(oldElement).before(newElement); $j(oldElement).appendTo(form); //set attributes $j(form).css('position', 'absolute'); $j(form).css('top', '-1200px'); $j(form).css('left', '-1200px'); $j(form).appendTo('body'); return form; }, ajaxFileUpload: function (s) { // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout s = $j.extend({}, $j.ajaxSettings, s); var id = new Date().getTime(); var form = $j.createUploadForm(id, s.fileElementId, (typeof (s.data) == 'undefined' ? false : s.data)); var io = $j.createUploadIframe(id, s.secureuri); var frameId = 'jUploadFrame' + id; var formId = 'jUploadForm' + id; // Watch for a new set of requests if (s.global && !$j.active++) { $j.event.trigger("ajaxStart"); } var requestDone = false; // Create the request object var xml = {}; if (s.global) $j.event.trigger("ajaxSend", [xml, s]); // Wait for a response to come back var uploadCallback = function (isTimeout) { var io = document.getElementById(frameId); try { if (io.contentWindow) { if (!$j.support.cssFloat) { xml.responseXML = io.contentWindow.document.body.innerHTML; } else { xml.responseXML = io.contentWindow.document.XMLDocument ? io.contentWindow.document.XMLDocument : io.contentWindow.document; } } else if (io.contentDocument) { xml.responseXML = io.contentDocument.document.XMLDocument ? io.contentDocument.document.XMLDocument : io.contentDocument.document; } } catch (e) { //$j.handleError(s, xml, null, e); } if (xml || isTimeout == "timeout") { requestDone = true; var status; try { status = isTimeout != "timeout" ? "success" : "error"; // Make sure that the request was successful or notmodified if (status != "error") { // process the data (runs the xml through httpData regardless of callback) var data = $j.uploadHttpData(xml); // If a local callback was specified, fire it and pass it the data if (s.success) s.success(data, status); // Fire the global callback if (s.global) $j.event.trigger("ajaxSuccess", [xml, s]); } //else //$j.handleError(s, xml, status); } catch (e) { status = "error"; //$j.handleError(s, xml, status, e); } // The request was completed if (s.global) $j.event.trigger("ajaxComplete", [xml, s]); // Handle the global AJAX counter if (s.global && ! --$j.active) $j.event.trigger("ajaxStop"); // Process result if (s.complete) s.complete(xml, status); $j(io).unbind(); setTimeout(function () { try { $j(io).remove(); $j(form).remove(); } catch (e) { //$j.handleError(s, xml, null, e); } }, 100); xml = null; } } // Timeout checker if (s.timeout > 0) { setTimeout(function () { // Check to see if the request is still happening if (!requestDone) uploadCallback("timeout"); }, s.timeout); } try { var form = $j('#' + formId); $j(form).attr('action', s.url); $j(form).attr('method', 'POST'); $j(form).attr('target', frameId); if (form.encoding) { $j(form).attr('encoding', 'multipart/form-data'); } else { $j(form).attr('enctype', 'multipart/form-data'); } $j(form).submit(); } catch (e) { //$j.handleError(s, xml, null, e); } $j('#' + frameId).load(uploadCallback); return { abort: function () { } }; }, uploadHttpData: function (r) { if (!$j.support.cssFloat) { return $j(r.responseXML).find(".tx").text(); } else { return $j(r.responseXML).find('UploadFileResult').text(); } } })