OSDN Git Service

94b7f0f7a07ea5d5aa8fb635de585f960d305498
[embrj/master.git] / js / ajaxfileupload.js
1 jQuery.extend({\r
2         createUploadIframe: function (id, uri) {\r
3                 //create frame\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
9                         }\r
10                         else if (typeof uri == 'string') {\r
11                                 io.src = uri;\r
12                         }\r
13                 }\r
14                 else {\r
15                         var io = document.createElement('iframe');\r
16                         io.id = frameId;\r
17                         io.name = frameId;\r
18                 }\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
23                 return io\r
24         },\r
25         createUploadForm: function (id, fileElementId) {\r
26                 //create form   \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
35                 //set attributes\r
36                 $(form).css('position', 'absolute');\r
37                 $(form).css('top', '-1200px');\r
38                 $(form).css('left', '-1200px');\r
39                 $(form).appendTo('body');\r
40                 return form;\r
41         },\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
53                 }\r
54                 var requestDone = false;\r
55                 // Create the request object\r
56                 var xml = {}\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
61                         try {\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
68                                 }\r
69                         } catch (e) {\r
70                                 jQuery.handleError(s, xml, null, e);\r
71                         }\r
72                         if (xml || isTimeout == "timeout") {\r
73                                 requestDone = true;\r
74                                 var status;\r
75                                 try {\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
86                                 } catch (e) {\r
87                                         status = "error";\r
88                                         jQuery.handleError(s, xml, status, e);\r
89                                 }\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
94                                 // Process result\r
95                                 if (s.complete) s.complete(xml, status);\r
96                                 jQuery(io).unbind()\r
97                                 setTimeout(function () {\r
98                                         try {\r
99                                                 $(io).remove();\r
100                                                 $(form).remove();\r
101                                         } catch (e) {\r
102                                                 jQuery.handleError(s, xml, null, e);\r
103                                         }\r
104                                 }, 100)\r
105                                 xml = null\r
106                         }\r
107                 }\r
108                 // Timeout checker\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
113                         }, s.timeout);\r
114                 }\r
115                 try {\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
123                         }\r
124                         else {\r
125                                 form.enctype = 'multipart/form-data';\r
126                         }\r
127                         $(form).submit();\r
128                 } catch (e) {\r
129                         jQuery.handleError(s, xml, null, e);\r
130                 }\r
131                 if (window.attachEvent) {\r
132                         document.getElementById(frameId).attachEvent('onload', uploadCallback);\r
133                 }\r
134                 else {\r
135                         document.getElementById(frameId).addEventListener('load', uploadCallback, false);\r
136                 }\r
137                 return {\r
138                         abort: function () {}\r
139                 };\r
140         },\r
141         uploadHttpData: function (r, type) {\r
142                 var data = !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
150                 return data;\r
151         }\r
152 })