2 createUploadIframe: function(id, uri){
4 var frameId = 'jUploadFrame' + id;
5 if(window.ActiveXObject) {
6 var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />');
7 if(typeof uri== 'boolean'){
8 io.src = 'javascript:false';
10 else if(typeof uri== 'string'){
15 var io = document.createElement('iframe');
19 io.style.position = 'absolute';
20 io.style.top = '-1000px';
21 io.style.left = '-1000px';
23 document.body.appendChild(io);
26 createUploadForm: function(id, fileElementId)
29 var formId = 'jUploadForm' + id;
30 var fileId = 'jUploadFile' + id;
31 var form = $('<form action="" method="POST" name="' + formId + '" id="' + formId
32 + '" enctype="multipart/form-data"></form>');
33 var oldElement = $('#' + fileElementId);
34 var newElement = $(oldElement).clone();
35 $(oldElement).attr('id', fileId);
36 $(oldElement).before(newElement);
37 $(oldElement).appendTo(form);
39 $(form).css('position', 'absolute');
40 $(form).css('top', '-1200px');
41 $(form).css('left', '-1200px');
42 $(form).appendTo('body');
46 ajaxFileUpload: function(s) {
47 // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout
48 s = jQuery.extend({}, jQuery.ajaxSettings, s);
49 var id = new Date().getTime()
50 var form = jQuery.createUploadForm(id, s.fileElementId);
51 var io = jQuery.createUploadIframe(id, s.secureuri);
52 var frameId = 'jUploadFrame' + id;
53 var formId = 'jUploadForm' + id;
54 // Watch for a new set of requests
55 if ( s.global && ! jQuery.active++ )
57 jQuery.event.trigger( "ajaxStart" );
59 var requestDone = false;
60 // Create the request object
63 jQuery.event.trigger("ajaxSend", [xml, s]);
64 // Wait for a response to come back
65 var uploadCallback = function(isTimeout)
67 var io = document.getElementById(frameId);
70 xml.responseText = io.contentWindow.document.body ?
71 io.contentWindow.document.body.innerText : null;
72 xml.responseXML = io.contentWindow.document.XMLDocument ?
73 io.contentWindow.document.XMLDocument : io.contentWindow.document;
76 else if(io.contentDocument)
78 xml.responseText = io.contentDocument.document.body ?
79 io.contentDocument.document.body.textContent || document.body.innerText : null;
80 xml.responseXML = io.contentDocument.document.XMLDocument ?
81 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()
135 jQuery.handleError(s, xml, null, e);
142 if ( s.timeout > 0 ) {
143 setTimeout(function(){
144 // Check to see if the request is still happening
145 if( !requestDone ) uploadCallback( "timeout" );
150 // var io = $('#' + frameId);
151 var form = $('#' + formId);
152 $(form).attr('action', s.url);
153 $(form).attr('method', 'POST');
154 $(form).attr('target', frameId);
157 form.encoding = 'multipart/form-data';
161 form.enctype = 'multipart/form-data';
167 jQuery.handleError(s, xml, null, e);
169 if(window.attachEvent){
170 document.getElementById(frameId).attachEvent('onload', uploadCallback);
173 document.getElementById(frameId).addEventListener('load', uploadCallback, false);
175 return {abort: function () {}};
179 uploadHttpData: function( r, type ) {
181 data = type == "xml" || data ? r.responseXML : r.responseText;
182 // If the type is "script", eval it in global context
183 if ( type == "script" )
184 jQuery.globalEval( data );
185 // Get the JavaScript object, if JSON is used.
186 if ( type == "json" )
187 eval( "data = " + data );
188 // evaluate scripts within html
189 if ( type == "html" )
190 jQuery("<div>").html(data).evalScripts();
191 //alert($('param', data).each(function(){alert($(this).attr('value'));}));