4 createUploadIframe: function(id, uri)
7 var frameId = 'jUploadFrame' + id;
9 if(window.ActiveXObject) {
10 var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
11 if(typeof uri== 'boolean'){
12 io.src = 'javascript:false';
14 else if(typeof uri== 'string'){
19 var io = document.createElement('iframe');
23 io.style.position = 'absolute';
24 io.style.top = '-1000px';
25 io.style.left = '-1000px';
27 document.body.appendChild(io);
31 createUploadForm: function(id, fileElementId)
34 var formId = 'jUploadForm' + id;
35 var fileId = 'jUploadFile' + id;
36 var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId + '" enctype="multipart/form-data"></form>');
37 var oldElement = $('#' + fileElementId);
38 var newElement = $(oldElement).clone();
39 $(oldElement).attr('id', fileId);
40 $(oldElement).before(newElement);
41 $(oldElement).appendTo(form);
43 $(form).css('position', 'absolute');
44 $(form).css('top', '-1200px');
45 $(form).css('left', '-1200px');
46 $(form).appendTo('body');
50 ajaxFileUpload: function(s) {
51 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
52 s = jQuery.extend({}, jQuery.ajaxSettings, s);
53 var id = new Date().getTime()
54 var form = jQuery.createUploadForm(id, s.fileElementId);
55 var io = jQuery.createUploadIframe(id, s.secureuri);
56 var frameId = 'jUploadFrame' + id;
57 var formId = 'jUploadForm' + id;
58 // Watch for a new set of requests
59 if ( s.global && ! jQuery.active++ )
61 jQuery.event.trigger( "ajaxStart" );
63 var requestDone = false;
64 // Create the request object
67 jQuery.event.trigger("ajaxSend", [xml, s]);
68 // Wait for a response to come back
69 var uploadCallback = function(isTimeout)
71 var io = document.getElementById(frameId);
76 xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
77 xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;
79 }else if(io.contentDocument)
81 xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
82 xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
86 jQuery.handleError(s, xml, null, e);
88 if ( xml || isTimeout == "timeout")
93 status = isTimeout != "timeout" ? "success" : "error";
94 // Make sure that the request was successful or notmodified
95 if ( status != "error" )
97 // process the data (runs the xml through httpData regardless of callback)
98 var data = jQuery.uploadHttpData( xml, s.dataType );
99 // If a local callback was specified, fire it and pass it the data
101 s.success( data, status );
103 // Fire the global callback
105 jQuery.event.trigger( "ajaxSuccess", [xml, s] );
107 jQuery.handleError(s, xml, status);
111 jQuery.handleError(s, xml, status, e);
114 // The request was completed
116 jQuery.event.trigger( "ajaxComplete", [xml, s] );
118 // Handle the global AJAX counter
119 if ( s.global && ! --jQuery.active )
120 jQuery.event.trigger( "ajaxStop" );
124 s.complete(xml, status);
128 setTimeout(function()
136 jQuery.handleError(s, xml, null, e);
148 setTimeout(function(){
149 // Check to see if the request is still happening
150 if( !requestDone ) uploadCallback( "timeout" );
155 // var io = $('#' + frameId);
156 var form = $('#' + formId);
157 $(form).attr('action', s.url);
158 $(form).attr('method', 'POST');
159 $(form).attr('target', frameId);
162 form.encoding = 'multipart/form-data';
166 form.enctype = 'multipart/form-data';
172 jQuery.handleError(s, xml, null, e);
174 if(window.attachEvent){
175 document.getElementById(frameId).attachEvent('onload', uploadCallback);
178 document.getElementById(frameId).addEventListener('load', uploadCallback, false);
180 return {abort: function () {}};
184 uploadHttpData: function( r, type ) {
186 data = type == "xml" || data ? r.responseXML : r.responseText;
187 // If the type is "script", eval it in global context
188 if ( type == "script" )
189 jQuery.globalEval( data );
190 // Get the JavaScript object, if JSON is used.
191 if ( type == "json" )
192 eval( "data = " + data );
193 // evaluate scripts within html
194 if ( type == "html" )
195 jQuery("<div>").html(data).evalScripts();
196 //alert($('param', data).each(function(){alert($(this).attr('value'));}));