正在显示
1 个修改的文件
包含
223 行增加
和
0 行删除
1 | +jQuery.extend({ | ||
2 | + | ||
3 | + | ||
4 | + createUploadIframe: function(id, uri) | ||
5 | + { | ||
6 | + //create frame | ||
7 | + var frameId = 'jUploadFrame' + id; | ||
8 | + | ||
9 | + if(window.ActiveXObject) { | ||
10 | + var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />'); | ||
11 | + if(typeof uri== 'boolean'){ | ||
12 | + io.src = 'javascript:false'; | ||
13 | + } | ||
14 | + else if(typeof uri== 'string'){ | ||
15 | + io.src = uri; | ||
16 | + } | ||
17 | + } | ||
18 | + else { | ||
19 | + var io = document.createElement('iframe'); | ||
20 | + io.id = frameId; | ||
21 | + io.name = frameId; | ||
22 | + } | ||
23 | + io.style.position = 'absolute'; | ||
24 | + io.style.top = '-1000px'; | ||
25 | + io.style.left = '-1000px'; | ||
26 | + | ||
27 | + document.body.appendChild(io); | ||
28 | + | ||
29 | + return io | ||
30 | + }, | ||
31 | + createUploadForm: function(id, fileElementId) | ||
32 | + { | ||
33 | + //create form | ||
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(true); | ||
39 | + $(oldElement).attr('id', fileId); | ||
40 | + $(oldElement).before(newElement); | ||
41 | + $(oldElement).appendTo(form); | ||
42 | + //set attributes | ||
43 | + $(form).css('position', 'absolute'); | ||
44 | + $(form).css('top', '-1200px'); | ||
45 | + $(form).css('left', '-1200px'); | ||
46 | + $(form).appendTo('body'); | ||
47 | + return form; | ||
48 | + }, | ||
49 | + addOtherRequestsToForm: function(form,data) | ||
50 | + { | ||
51 | + // add extra parameter | ||
52 | + var originalElement = $('<input type="hidden" name="" value="">'); | ||
53 | + for (var key in data) { | ||
54 | + name = key; | ||
55 | + value = data[key]; | ||
56 | + var cloneElement = originalElement.clone(); | ||
57 | + cloneElement.attr({'name':name,'value':value}); | ||
58 | + $(cloneElement).appendTo(form); | ||
59 | + } | ||
60 | + return form; | ||
61 | + }, | ||
62 | + | ||
63 | + ajaxFileUpload: function(s) { | ||
64 | + // TODO introduce global settings, allowing the client to modify them for all requests, not only timeout | ||
65 | + s = jQuery.extend({}, jQuery.ajaxSettings, s); | ||
66 | + var id = new Date().getTime() | ||
67 | + var form = jQuery.createUploadForm(id, s.fileElementId); | ||
68 | + if ( s.data ) form = jQuery.addOtherRequestsToForm(form,s.data); | ||
69 | + var io = jQuery.createUploadIframe(id, s.secureuri); | ||
70 | + var frameId = 'jUploadFrame' + id; | ||
71 | + var formId = 'jUploadForm' + id; | ||
72 | + // Watch for a new set of requests | ||
73 | + if ( s.global && ! jQuery.active++ ) | ||
74 | + { | ||
75 | + jQuery.event.trigger( "ajaxStart" ); | ||
76 | + } | ||
77 | + var requestDone = false; | ||
78 | + // Create the request object | ||
79 | + var xml = {} | ||
80 | + if ( s.global ) | ||
81 | + jQuery.event.trigger("ajaxSend", [xml, s]); | ||
82 | + // Wait for a response to come back | ||
83 | + var uploadCallback = function(isTimeout) | ||
84 | + { | ||
85 | + var io = document.getElementById(frameId); | ||
86 | + try | ||
87 | + { | ||
88 | + if(io.contentWindow) | ||
89 | + { | ||
90 | + xml.responseText = io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null; | ||
91 | + xml.responseXML = io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document; | ||
92 | + | ||
93 | + }else if(io.contentDocument) | ||
94 | + { | ||
95 | + xml.responseText = io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null; | ||
96 | + xml.responseXML = io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document; | ||
97 | + } | ||
98 | + }catch(e) | ||
99 | + { | ||
100 | + jQuery.handleError(s, xml, null, e); | ||
101 | + } | ||
102 | + if ( xml || isTimeout == "timeout") | ||
103 | + { | ||
104 | + requestDone = true; | ||
105 | + var status; | ||
106 | + try { | ||
107 | + status = isTimeout != "timeout" ? "success" : "error"; | ||
108 | + // Make sure that the request was successful or notmodified | ||
109 | + if ( status != "error" ) | ||
110 | + { | ||
111 | + // process the data (runs the xml through httpData regardless of callback) | ||
112 | + var data = jQuery.uploadHttpData( xml, s.dataType ); | ||
113 | + // If a local callback was specified, fire it and pass it the data | ||
114 | + if ( s.success ) | ||
115 | + s.success( data, status ); | ||
116 | + | ||
117 | + // Fire the global callback | ||
118 | + if( s.global ) | ||
119 | + jQuery.event.trigger( "ajaxSuccess", [xml, s] ); | ||
120 | + } else | ||
121 | + jQuery.handleError(s, xml, status); | ||
122 | + } catch(e) | ||
123 | + { | ||
124 | + status = "error"; | ||
125 | + jQuery.handleError(s, xml, status, e); | ||
126 | + } | ||
127 | + | ||
128 | + // The request was completed | ||
129 | + if( s.global ) | ||
130 | + jQuery.event.trigger( "ajaxComplete", [xml, s] ); | ||
131 | + | ||
132 | + // Handle the global AJAX counter | ||
133 | + if ( s.global && ! --jQuery.active ) | ||
134 | + jQuery.event.trigger( "ajaxStop" ); | ||
135 | + | ||
136 | + // Process result | ||
137 | + if ( s.complete ) | ||
138 | + s.complete(xml, status); | ||
139 | + | ||
140 | + jQuery(io).unbind() | ||
141 | + | ||
142 | + setTimeout(function() | ||
143 | + { try | ||
144 | + { | ||
145 | + $(io).remove(); | ||
146 | + $(form).remove(); | ||
147 | + | ||
148 | + } catch(e) | ||
149 | + { | ||
150 | + jQuery.handleError(s, xml, null, e); | ||
151 | + } | ||
152 | + | ||
153 | + }, 100) | ||
154 | + | ||
155 | + xml = null | ||
156 | + | ||
157 | + } | ||
158 | + } | ||
159 | + // Timeout checker | ||
160 | + if ( s.timeout > 0 ) | ||
161 | + { | ||
162 | + setTimeout(function(){ | ||
163 | + // Check to see if the request is still happening | ||
164 | + if( !requestDone ) uploadCallback( "timeout" ); | ||
165 | + }, s.timeout); | ||
166 | + } | ||
167 | + try | ||
168 | + { | ||
169 | + // var io = $('#' + frameId); | ||
170 | + var form = $('#' + formId); | ||
171 | + $(form).attr('action', s.url); | ||
172 | + $(form).attr('method', 'POST'); | ||
173 | + $(form).attr('target', frameId); | ||
174 | + if(form.encoding) | ||
175 | + { | ||
176 | + form.encoding = 'multipart/form-data'; | ||
177 | + } | ||
178 | + else | ||
179 | + { | ||
180 | + form.enctype = 'multipart/form-data'; | ||
181 | + } | ||
182 | + $(form).submit(); | ||
183 | + | ||
184 | + } catch(e) | ||
185 | + { | ||
186 | + jQuery.handleError(s, xml, null, e); | ||
187 | + } | ||
188 | + if(window.attachEvent){ | ||
189 | + document.getElementById(frameId).attachEvent('onload', uploadCallback); | ||
190 | + } | ||
191 | + else{ | ||
192 | + document.getElementById(frameId).addEventListener('load', uploadCallback, false); | ||
193 | + } | ||
194 | + return {abort: function () {}}; | ||
195 | + | ||
196 | + }, | ||
197 | + | ||
198 | + uploadHttpData: function( r, type ) { | ||
199 | + var data = !type; | ||
200 | + data = type == "xml" || data ? r.responseXML : r.responseText; | ||
201 | + // If the type is "script", eval it in global context | ||
202 | + if ( type == "script" ) | ||
203 | + jQuery.globalEval( data ); | ||
204 | + // Get the JavaScript object, if JSON is used. | ||
205 | + if ( type == "json" ) | ||
206 | + { | ||
207 | + // If you add mimetype in your response, | ||
208 | + // you have to delete the '<pre></pre>' tag. | ||
209 | + // The pre tag in Chrome has attribute, so have to use regex to remove | ||
210 | + var data = r.responseText; | ||
211 | + var rx = new RegExp("<pre.*?>(.*?)</pre>","i"); | ||
212 | + var am = rx.exec(data); | ||
213 | + //this is the desired data extracted | ||
214 | + var data = (am) ? am[1] : ""; //the only submatch or empty | ||
215 | + eval( "data = " + data ); | ||
216 | + } | ||
217 | + // evaluate scripts within html | ||
218 | + if ( type == "html" ) | ||
219 | + jQuery("<div>").html(data).evalScripts(); | ||
220 | + //alert($('param', data).each(function(){alert($(this).attr('value'));})); | ||
221 | + return data; | ||
222 | + } | ||
223 | +}) |
-
请 注册 或 登录 后发表评论