1 | /**
|
---|
2 | * Flash Multi Upload
|
---|
3 | *
|
---|
4 | * Based on a example from Alastair Dawson
|
---|
5 | *
|
---|
6 | * @link http://blog.vixiom.com/2006/09/08/multiple-file-upload-with-flash-and-ruby-on-rails/
|
---|
7 | * @author Alastair Dawson
|
---|
8 | * @author Andreas Gohr <[email protected]>
|
---|
9 | * @license MIT <http://www.opensource.org/licenses/mit-license.php>
|
---|
10 | */
|
---|
11 |
|
---|
12 | // delegate
|
---|
13 | import mx.utils.Delegate;
|
---|
14 | // ui components
|
---|
15 | import mx.controls.DataGrid;
|
---|
16 | import mx.controls.gridclasses.DataGridColumn
|
---|
17 | import mx.controls.Button;
|
---|
18 | import mx.controls.TextInput;
|
---|
19 | import mx.controls.CheckBox;
|
---|
20 | import mx.controls.Label;
|
---|
21 | // file reference
|
---|
22 | import flash.net.FileReferenceList;
|
---|
23 | import flash.net.FileReference;
|
---|
24 |
|
---|
25 | class MultipleUpload {
|
---|
26 |
|
---|
27 | private var fileRef:FileReferenceList;
|
---|
28 | private var fileRefListener:Object;
|
---|
29 | private var list:Array;
|
---|
30 | private var dp:Array;
|
---|
31 |
|
---|
32 | private var files_dg:DataGrid;
|
---|
33 | private var browse_btn:Button;
|
---|
34 | private var upload_btn:Button;
|
---|
35 | private var ns_input:TextInput;
|
---|
36 | private var ns_label:Label;
|
---|
37 | private var overwrite_cb:CheckBox;
|
---|
38 |
|
---|
39 | private var url:String;
|
---|
40 | private var upurl:String;
|
---|
41 | private var current:Number;
|
---|
42 | private var done:Number;
|
---|
43 | private var lasterror:String;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Constructor.
|
---|
47 | *
|
---|
48 | * Initializes the needed objects and stage objects
|
---|
49 | */
|
---|
50 | public function MultipleUpload(fdg:DataGrid, bb:Button, ub:Button, nsi:TextInput, nsl:Label, ob:CheckBox) {
|
---|
51 | // references for objects on the stage
|
---|
52 | files_dg = fdg;
|
---|
53 | browse_btn = bb;
|
---|
54 | upload_btn = ub;
|
---|
55 | ns_input = nsi;
|
---|
56 | ns_label = nsl;
|
---|
57 | overwrite_cb = ob;
|
---|
58 |
|
---|
59 | // file list references & listener
|
---|
60 | fileRef = new FileReferenceList();
|
---|
61 | fileRefListener = new Object();
|
---|
62 | fileRef.addListener(fileRefListener);
|
---|
63 |
|
---|
64 | // setup
|
---|
65 | iniUI();
|
---|
66 | inifileRefListener();
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Initializes the User Interface
|
---|
71 | *
|
---|
72 | * Uses flashvars to access possibly localized names
|
---|
73 | */
|
---|
74 | private function iniUI() {
|
---|
75 | // register button handlers
|
---|
76 | browse_btn.onRelease = Delegate.create(this, this.browse);
|
---|
77 | upload_btn.onRelease = Delegate.create(this, this.upload);
|
---|
78 |
|
---|
79 | // columns for dataGrid
|
---|
80 | var col:DataGridColumn;
|
---|
81 | col = new DataGridColumn('name');
|
---|
82 | col.headerText = ( _root.L_gridname ? _root.L_gridname : 'Filename' );
|
---|
83 | col.sortable = false;
|
---|
84 | files_dg.addColumn(col);
|
---|
85 | col = new DataGridColumn('size');
|
---|
86 | col.headerText = ( _root.L_gridsize ? _root.L_gridsize : 'Size' );
|
---|
87 | col.sortable = false;
|
---|
88 | files_dg.addColumn(col);
|
---|
89 | col = new DataGridColumn('status');
|
---|
90 | col.headerText = ( _root.L_gridstat ? _root.L_gridstat : 'Status' );
|
---|
91 | col.sortable = false;
|
---|
92 | files_dg.addColumn(col);
|
---|
93 |
|
---|
94 | // label translations
|
---|
95 | if(_root.L_overwrite) overwrite_cb.label = _root.L_overwrite;
|
---|
96 | if(_root.L_browse) browse_btn.label = _root.L_browse;
|
---|
97 | if(_root.L_upload) upload_btn.label = _root.L_upload;
|
---|
98 | if(_root.L_namespace) ns_label.text = _root.L_namespace;
|
---|
99 |
|
---|
100 | // prefill input field
|
---|
101 | if(_root.O_ns) ns_input.text = _root.O_ns;
|
---|
102 |
|
---|
103 | // disable buttons
|
---|
104 | upload_btn.enabled = false;
|
---|
105 | if(!_root.O_overwrite) overwrite_cb.visible = false;
|
---|
106 |
|
---|
107 | // initalize the data provider list
|
---|
108 | dp = new Array();
|
---|
109 | list = new Array();
|
---|
110 | files_dg.spaceColumnsEqually();
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Open files selection dialog
|
---|
115 | *
|
---|
116 | * Adds the allowed file types
|
---|
117 | */
|
---|
118 | private function browse() {
|
---|
119 | if(_root.O_extensions){
|
---|
120 | var exts:Array = _root.O_extensions.split('|');
|
---|
121 | var filter:Object = new Object();
|
---|
122 | filter.description = (_root.L_filetypes ? _root.L_filetypes : 'Allowed filetypes');
|
---|
123 | filter.extension = '';
|
---|
124 | for(var i:Number = 0; i<exts.length; i++){
|
---|
125 | filter.extension += '*.'+exts[i]+';';
|
---|
126 | }
|
---|
127 | filter.extension = filter.extension.substr(0,filter.extension.length-1);
|
---|
128 | var apply:Array = new Array();
|
---|
129 | apply.push(filter);
|
---|
130 | fileRef.browse(apply);
|
---|
131 | }else{
|
---|
132 | fileRef.browse();
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Initiates the upload process
|
---|
138 | */
|
---|
139 | private function upload() {
|
---|
140 | // prepare backend URL
|
---|
141 | this.url = _root.O_backend; // from flashvars
|
---|
142 | this.url += '&ns='+escape(ns_input.text);
|
---|
143 |
|
---|
144 | // prepare upload url
|
---|
145 | this.upurl = this.url;
|
---|
146 | this.upurl += '§ok='+escape(_root.O_sectok);
|
---|
147 | this.upurl += '&authtok='+escape(_root.O_authtok);
|
---|
148 | if(overwrite_cb.selected) this.upurl += '&ow=1';
|
---|
149 |
|
---|
150 | // disable buttons
|
---|
151 | upload_btn.enabled = false;
|
---|
152 | browse_btn.enabled = false;
|
---|
153 | ns_input.enabled = false;
|
---|
154 | overwrite_cb.enabled = false;
|
---|
155 |
|
---|
156 | // init states
|
---|
157 | this.current = -1;
|
---|
158 | this.done = 0;
|
---|
159 | this.lasterror = '';
|
---|
160 |
|
---|
161 | // start process detached
|
---|
162 | _global.setTimeout(this,'uploadNext',100);
|
---|
163 | nextFrame();
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Uploads the next file in the list
|
---|
168 | */
|
---|
169 | private function uploadNext(){
|
---|
170 | this.current++;
|
---|
171 | if(this.current >= this.list.length){
|
---|
172 | return this.uploadDone();
|
---|
173 | }
|
---|
174 |
|
---|
175 | var file = this.list[this.current];
|
---|
176 |
|
---|
177 | if(_root.O_maxsize && (file.size > _root.O_maxsize)){
|
---|
178 | this.lasterror = (_root.L_toobig ? _root.L_toobig : 'too big');
|
---|
179 | _global.setTimeout(this,'uploadNext',100);
|
---|
180 | nextFrame();
|
---|
181 | }else{
|
---|
182 | file.addListener(fileRefListener);
|
---|
183 | file.upload(upurl);
|
---|
184 | // continues in the handlers
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Redirect to the namespace and set a success/error message
|
---|
190 | *
|
---|
191 | * Called when all files in the list where processed
|
---|
192 | */
|
---|
193 | private function uploadDone(){
|
---|
194 | var info = (_root.L_info ? _root.L_info : 'files uploaded');
|
---|
195 | if(this.done == this.list.length){
|
---|
196 | this.url += '&msg1='+escape(this.done+'/'+this.list.length+' '+info);
|
---|
197 | }else{
|
---|
198 | var lasterr = (_root.L_lasterr ? _root.L_lasterr : 'Last error:');
|
---|
199 | this.url += '&err='+escape(this.done+'/'+this.list.length+' '+info+' '+lasterr+' '+this.lasterror);
|
---|
200 | }
|
---|
201 |
|
---|
202 | // when done redirect
|
---|
203 | getURL(this.url,'_self');
|
---|
204 | }
|
---|
205 |
|
---|
206 | /**
|
---|
207 | * Set the status of a given file in the data grid
|
---|
208 | */
|
---|
209 | private function setStatus(file,msg){
|
---|
210 | for(var i:Number = 0; i < list.length; i++) {
|
---|
211 | if (list[i].name == file.name) {
|
---|
212 | files_dg.editField(i, 'status', msg);
|
---|
213 | nextFrame();
|
---|
214 | return;
|
---|
215 | }
|
---|
216 | }
|
---|
217 | }
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Initialize the file reference listener
|
---|
221 | */
|
---|
222 | private function inifileRefListener() {
|
---|
223 | fileRefListener.onSelect = Delegate.create(this, this.onSelect);
|
---|
224 | fileRefListener.onCancel = Delegate.create(this, this.onCancel);
|
---|
225 | fileRefListener.onOpen = Delegate.create(this, this.onOpen);
|
---|
226 | fileRefListener.onProgress = Delegate.create(this, this.onProgress);
|
---|
227 | fileRefListener.onComplete = Delegate.create(this, this.onComplete);
|
---|
228 | fileRefListener.onHTTPError = Delegate.create(this, this.onHTTPError);
|
---|
229 | fileRefListener.onIOError = Delegate.create(this, this.onIOError);
|
---|
230 | fileRefListener.onSecurityError = Delegate.create(this, this.onSecurityError);
|
---|
231 | }
|
---|
232 |
|
---|
233 | /**
|
---|
234 | * Handle file selection
|
---|
235 | *
|
---|
236 | * Files are added as in a list of references and beautified into the data grid dataprovider array
|
---|
237 | *
|
---|
238 | * Multiple browses will add to the list
|
---|
239 | */
|
---|
240 | private function onSelect(fileRefList:FileReferenceList) {
|
---|
241 | var sel = fileRefList.fileList;
|
---|
242 | for(var i:Number = 0; i < sel.length; i++) {
|
---|
243 | // check size
|
---|
244 | var stat:String;
|
---|
245 | if(_root.O_maxsize && sel[i].size > _root.O_maxsize){
|
---|
246 | stat = (_root.L_toobig ? _root.L_toobig : 'too big');
|
---|
247 | }else{
|
---|
248 | stat = (_root.L_ready ? _root.L_ready : 'ready for upload');
|
---|
249 | }
|
---|
250 | // add to grid
|
---|
251 | dp.push({name:sel[i].name, size:Math.round(sel[i].size / 1000) + " kb", status:stat});
|
---|
252 | // add to reference list
|
---|
253 | list.push(sel[i]);
|
---|
254 | }
|
---|
255 | // update dataGrid
|
---|
256 | files_dg.dataProvider = dp;
|
---|
257 | files_dg.spaceColumnsEqually();
|
---|
258 |
|
---|
259 | if(list.length > 0) upload_btn.enabled = true;
|
---|
260 | }
|
---|
261 |
|
---|
262 | /**
|
---|
263 | * Does nothing
|
---|
264 | */
|
---|
265 | private function onCancel() {
|
---|
266 | }
|
---|
267 |
|
---|
268 | /**
|
---|
269 | * Does nothing
|
---|
270 | */
|
---|
271 | private function onOpen(file:FileReference) {
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Set the upload progress
|
---|
276 | */
|
---|
277 | private function onProgress(file:FileReference, bytesLoaded:Number, bytesTotal:Number) {
|
---|
278 | var percentDone = Math.round((bytesLoaded / bytesTotal) * 100);
|
---|
279 | var msg:String = 'uploading @PCT@%';
|
---|
280 | if(_root.L_progress) msg = _root.L_progress;
|
---|
281 | msg = msg.split('@PCT@').join(percentDone);
|
---|
282 | this.setStatus(file,msg);
|
---|
283 | }
|
---|
284 |
|
---|
285 | /**
|
---|
286 | * Handle upload completion
|
---|
287 | */
|
---|
288 | private function onComplete(file:FileReference) {
|
---|
289 | this.setStatus(file,(_root.L_done ? _root.L_done : 'complete'));
|
---|
290 | this.done++;
|
---|
291 | uploadNext();
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * Handle upload errors
|
---|
296 | */
|
---|
297 | private function onHTTPError(file:FileReference, httpError:Number) {
|
---|
298 | var error;
|
---|
299 | if(httpError == 400){
|
---|
300 | error = (_root.L_fail ? _root.L_fail : 'failed');
|
---|
301 | }else if(httpError == 401){
|
---|
302 | error = (_root.L_authfail ? _root.L_authfail : 'auth failed');
|
---|
303 | }else{
|
---|
304 | error = "HTTP Error " + httpError
|
---|
305 | }
|
---|
306 | this.setStatus(file,error);
|
---|
307 | this.lasterror = error;
|
---|
308 | uploadNext();
|
---|
309 | }
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * Handle IO errors
|
---|
313 | */
|
---|
314 | private function onIOError(file:FileReference) {
|
---|
315 | this.setStatus(file,"IO Error");
|
---|
316 | this.lasterror = "IO Error";
|
---|
317 | uploadNext();
|
---|
318 | }
|
---|
319 |
|
---|
320 | /**
|
---|
321 | * Handle Security errors
|
---|
322 | */
|
---|
323 | private function onSecurityError(file:FileReference, errorString:String) {
|
---|
324 | this.setStatus(file,"SecurityError: " + errorString);
|
---|
325 | this.lasterror = "SecurityError: " + errorString;
|
---|
326 | uploadNext();
|
---|
327 | }
|
---|
328 |
|
---|
329 |
|
---|
330 | }
|
---|