Subversion Repositories linkit

[/] [trunk/] [fileUploadApp/] [src/] [main/] [flex/] [com/] [flashdev/] [file/] [FileUpload.mxml] - Blame information for rev 1

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1 sbahloul
2
3
        layout="vertical" width="100%" minWidth="400" height="100%" minHeight="200"
4
        title="{Localizator.getInstance().getText('msg.welcome')}" creationComplete="initCom()">
5
 
6
        
7
                [Event(name="uploadComplete", type="flash.events.Event")]
8
                [Event(name="uploadProgress", type="flash.events.ProgressEvent")]
9
                [Event(name="uploadCancel", type="flash.events.Event")]
10
                [Event(name="uploadIOError", type="flash.events.IOErrorEvent")]
11
                [Event(name="uploadSecurityError", type="flash.events.SecurityErrorEvent")]
12
        
13
 
14
        
15
                
16
 
17
                        /*
18
 
19
                        Written by:
20
                        Dustin Andrew
21
                        dustin@flash-dev.com
22
                        www.flash-dev.com
23
 
24
                        FileUpload
25
 
26
                        Panel component for uploading files.
27
                        (Icons from http://www.famfamfam.com)
28
 
29
                        LAST UPDATED:
30
                        12/15/06
31
 
32
                        */
33
 
34
                        import mx.controls.*;
35
                        import mx.managers.*;
36
            import mx.events.*;
37
                        import flash.events.*;
38
                        import flash.net.*;
39
            import com.flashdev.utils.Localizator;
40
 
41
            [Bindable]
42
                        private var _strUploadUrl:String;
43
            [Bindable]
44
            private var _maxLeftSpace:Number;
45
            [Bindable]
46
            private var _maxFileSize:Number;
47
            [Bindable]
48
            private var _showSendButton:Boolean;
49
 
50
                        private var _refAddFiles:FileReferenceList;
51
                        private var _refUploadFile:FileReference;
52
                        private var _arrUploadFiles:Array;
53
                        private var _numCurrentUpload:Number = 0;
54
 
55
            private var _methodHasBeenCalled:Boolean = false;
56
 
57
                        // Set uploadUrl
58
                        public function set uploadUrl(strUploadUrl:String):void {
59
                _methodHasBeenCalled = true;
60
                                _strUploadUrl = strUploadUrl;
61
                        }
62
 
63
            // Set leftSpace
64
            public function set leftSpace(leftSpace:Number):void {
65
                _maxLeftSpace = leftSpace;
66
            }
67
 
68
            // Set maxFileSize
69
            public function set maxFileSize(maxFileSize:Number):void {
70
                _maxFileSize = maxFileSize;
71
            }
72
 
73
            // Set showSendButton
74
            public function set showSendButton(showSendButton:String):void {
75
                _showSendButton = ("true" == showSendButton);
76
            }
77
 
78
                        // Initalize
79
                        private function initCom():void {
80
                ExternalInterface.addCallback("sendFiles", sendFiles);
81
                                _arrUploadFiles = new Array();
82
                                enableUI();
83
                                uploadCheck();
84
                        }
85
 
86
            private function sendFiles():Boolean {
87
                if (_refAddFiles != null && _refAddFiles.fileList != null &&_refAddFiles.fileList.length > 0) {
88
                    return  startUpload();
89
                } else {
90
                    Alert.show(Localizator.getInstance().getText("msg.upload.nofile"));
91
                    return false;
92
                }
93
            }
94
 
95
                        // Called to add file(s) for upload
96
                        private function addFiles():void {
97
                                _refAddFiles = new FileReferenceList();
98
                                _refAddFiles.addEventListener(Event.SELECT, onSelectFile);
99
                                _refAddFiles.browse();
100
                        }
101
 
102
                        // Called when a file is selected
103
                        private function onSelectFile(event:Event):void {
104
                                var arrFoundList:Array = new Array();
105
                                // Get list of files from fileList, make list of files already on upload list
106
                                for (var i:Number = 0; i < _arrUploadFiles.length; i++) {
107
                                        for (var j:Number = 0; j < _refAddFiles.fileList.length; j++) {
108
                                                if (_arrUploadFiles[i].name == _refAddFiles.fileList[j].name) {
109
                                                        arrFoundList.push(_refAddFiles.fileList[j].name);
110
                                                        _refAddFiles.fileList.splice(j, 1);
111
                                                        j--;
112
                                                }
113
                                        }
114
                                }
115
                                if (_refAddFiles.fileList.length >= 1) {
116
                                        for (var k:Number = 0; k < _refAddFiles.fileList.length; k++) {
117
                        // check if the file size is > maxFileSize
118
                        if (_refAddFiles.fileList[k].size > _maxFileSize) {
119
                            var message:String = Localizator.getInstance().getText("msg.add.fileTooLarge");
120
                            message += " : ";
121
                            message += _refAddFiles.fileList[k].name;
122
                            message += " (";
123
                            message += formatFileSize(_refAddFiles.fileList[k].size);
124
                            message += ")\n";
125
                            message += Localizator.getInstance().getText("msg.add.maxFileSize");
126
                            message += " : ";
127
                            message += formatFileSize(_maxFileSize);
128
                            Alert.show(message);
129
                        } else {
130
                            _arrUploadFiles.push({
131
                                name:_refAddFiles.fileList[k].name,
132
                                size:formatFileSize(_refAddFiles.fileList[k].size),
133
                                file:_refAddFiles.fileList[k]});
134
                        }
135
                                        }
136
                                        listFiles.dataProvider = _arrUploadFiles;
137
                                        listFiles.selectedIndex = _arrUploadFiles.length - 1;
138
                                }
139
                                if (arrFoundList.length >= 1) {
140
                    var message:String = Localizator.getInstance().getText("msg.add.duplicate.file");
141
                    message += " : \n\n";
142
                    message += arrFoundList.join("\n• ");
143
                    message += "\n\n";
144
                    message += Localizator.getInstance().getText("msg.add.duplicate.main");
145
                                        Alert.show(message, Localizator.getInstance().getText("msg.add.duplicate.topic"));
146
                                }
147
                                updateProgBar();
148
                                scrollFiles();
149
                                uploadCheck();
150
                        }
151
 
152
                        // Called to format number to file size
153
                        private function formatFileSize(numSize:Number):String {
154
                                var strReturn:String;
155
                                numSize = Number(numSize / 1024);
156
                                strReturn = String(numSize.toFixed(1) + " KB");
157
                                if (numSize > 1024) {
158
                                        numSize = numSize / 1024;
159
                                        strReturn = String(numSize.toFixed(1) + " MB");
160
                                        if (numSize > 1024) {
161
                                                numSize = numSize / 1024;
162
                                                strReturn = String(numSize.toFixed(1) + " GB");
163
                                        }
164
                                }
165
                                return strReturn;
166
                        }
167
 
168
                        // Called to remove selected file(s) for upload
169
                        private function removeFiles():void {
170
                                var arrSelected:Array = listFiles.selectedIndices;
171
                                if (arrSelected.length >= 1) {
172
                                        for (var i:Number = 0; i < arrSelected.length; i++) {
173
                                                _arrUploadFiles[Number(arrSelected[i])] = null;
174
                                        }
175
                                        for (var j:Number = 0; j < _arrUploadFiles.length; j++) {
176
                                                if (_arrUploadFiles[j] == null) {
177
                                                        _arrUploadFiles.splice(j, 1);
178
                                                        j--;
179
                                                }
180
                                        }
181
                                        listFiles.dataProvider = _arrUploadFiles;
182
                                        listFiles.selectedIndex = 0;
183
                                }
184
                                updateProgBar();
185
                                scrollFiles();
186
                                uploadCheck();
187
                        }
188
 
189
                        // Called to check if there is at least one file to upload
190
                        private function uploadCheck():void {
191
                                if (_arrUploadFiles.length == 0) {
192
                                        btnUpload.enabled = false;
193
                                        listFiles.verticalScrollPolicy = "off";
194
                                } else {
195
                                        btnUpload.enabled = true;
196
                                        listFiles.verticalScrollPolicy = "on";
197
                                }
198
                        }
199
 
200
                        // Disable UI control
201
                        private function disableUI():void {
202
                                btnAdd.enabled = false;
203
                                btnRemove.enabled = false;
204
                                btnUpload.enabled = false;
205
                                btnCancel.enabled = true;
206
                                listFiles.enabled = false;
207
                                listFiles.verticalScrollPolicy = "off";
208
                        }
209
 
210
                        // Enable UI control
211
                        private function enableUI():void {
212
                                btnAdd.enabled = true;
213
                                btnRemove.enabled = true;
214
                                btnUpload.enabled = true;
215
                                btnCancel.enabled = false;
216
                                listFiles.enabled = true;
217
                                listFiles.verticalScrollPolicy = "on";
218
                        }
219
 
220
                        // Scroll listFiles to selected row
221
                        private function scrollFiles():void {
222
                                listFiles.verticalScrollPosition = listFiles.selectedIndex;
223
                                listFiles.validateNow();
224
                        }
225
 
226
                        // Called to upload file based on current upload number
227
                        public function startUpload():Boolean {
228
                                if (_arrUploadFiles.length > 0) {
229
                    var totalSize:Number = sumFileSize();
230
                    if (totalSize < _maxLeftSpace) {
231
                        disableUI();
232
 
233
                        listFiles.selectedIndex = _numCurrentUpload;
234
                        scrollFiles();
235
 
236
                        // Variables to send along with upload
237
                        var sendVars:URLVariables = new URLVariables();
238
                        sendVars.action = "upload";
239
 
240
                        var request:URLRequest = new URLRequest();
241
                        request.data = sendVars;
242
                        request.url = _strUploadUrl;
243
                        request.method = URLRequestMethod.POST;
244
                        _refUploadFile = new FileReference();
245
                        _refUploadFile = listFiles.selectedItem.file;
246
                        _refUploadFile.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
247
                        _refUploadFile.addEventListener(Event.COMPLETE, onUploadComplete);
248
                        _refUploadFile.addEventListener(IOErrorEvent.IO_ERROR, onUploadIoError);
249
                        _refUploadFile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadSecurityError);
250
                        _refUploadFile.upload(request, "file", false);
251
                        return true;
252
                    } else {
253
                        var message:String = Localizator.getInstance().getText("msg.upload.notEnoughSpace");
254
                        message += "\n";
255
                        message += Localizator.getInstance().getText("msg.upload.totalSize");
256
                        message += " : ";
257
                        message += formatFileSize(totalSize);
258
                        message += "\n";
259
                        message += Localizator.getInstance().getText("msg.upload.leftSpace");
260
                        message += " : ";
261
                        message += formatFileSize(_maxLeftSpace);
262
                        Alert.show(message);
263
                        return false;
264
                    }
265
                } else {
266
                    return false;
267
                }
268
            }
269
 
270
            private function doNothing():void { }
271
 
272
            private function sumFileSize():Number {
273
                var totalSize:Number = 0;
274
                for (var i:Number = 0; i < _arrUploadFiles.length; i++) {
275
                    totalSize += _arrUploadFiles[i].file.size;
276
                }
277
                return totalSize;
278
            }
279
 
280
                        // Cancel and clear eventlisteners on last upload
281
                        private function clearUpload():void {
282
                                _refUploadFile.removeEventListener(ProgressEvent.PROGRESS, onUploadProgress);
283
                                _refUploadFile.removeEventListener(Event.COMPLETE, onUploadComplete);
284
                                _refUploadFile.removeEventListener(IOErrorEvent.IO_ERROR, onUploadIoError);
285
                                _refUploadFile.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadSecurityError);
286
                                _refUploadFile.cancel();
287
                                _numCurrentUpload = 0;
288
                                updateProgBar();
289
                                enableUI();
290
                        }
291
 
292
                        // Called on upload cancel
293
                        private function onUploadCanceled():void {
294
                                clearUpload();
295
                                dispatchEvent(new Event("uploadCancel"));
296
                        }
297
 
298
                        // Get upload progress
299
                        private function onUploadProgress(event:ProgressEvent):void {
300
                                var numPerc:Number = Math.round((event.bytesLoaded / event.bytesTotal) * 100);
301
                                updateProgBar(numPerc);
302
                                var evt:ProgressEvent = new ProgressEvent("uploadProgress", false, false, event.bytesLoaded, event.bytesTotal);
303
                                dispatchEvent(evt);
304
                        }
305
 
306
                        // Update progBar
307
                        private function updateProgBar(numPerc:Number = 0):void {
308
                                var strLabel:String = (_numCurrentUpload + 1) + "/" + _arrUploadFiles.length;
309
                                strLabel = (_numCurrentUpload + 1 <= _arrUploadFiles.length && numPerc > 0 && numPerc < 100) ? numPerc + "% - " + strLabel : strLabel;
310
                                strLabel = (_numCurrentUpload + 1 == _arrUploadFiles.length && numPerc == 100) ? Localizator.getInstance().getText("msg.upload.complete") + " - " + strLabel : strLabel;
311
                                strLabel = (_arrUploadFiles.length == 0) ? "" : strLabel;
312
                                progBar.label = strLabel;
313
                                progBar.setProgress(numPerc, 100);
314
                                progBar.validateNow();
315
                        }
316
 
317
                        // Called on upload complete
318
                        private function onUploadComplete(event:Event):void {
319
                                _numCurrentUpload++;
320
                                if (_numCurrentUpload < _arrUploadFiles.length) {
321
                                        startUpload();
322
                                } else {
323
                    resetFilesList();
324
                                        enableUI();
325
                                        clearUpload();
326
                    dispatchEvent(new Event("uploadComplete"));
327
                    notifyUploadComplete();
328
                                }
329
                        }
330
 
331
            /** Call javascript method processUploadCompleteEvent()
332
             */
333
            private function notifyUploadComplete():void {
334
                if (ExternalInterface.available) {
335
                    ExternalInterface.call("processUploadCompleteEvent", "Upload complete !");
336
                }
337
            }
338
 
339
 
340
            // reset files list :
341
            private function resetFilesList():void {
342
                _arrUploadFiles = new Array();
343
                listFiles.dataProvider = _arrUploadFiles;
344
                listFiles.selectedIndex = 0;
345
                scrollFiles();
346
            }
347
 
348
                        // Called on upload io error
349
                        private function onUploadIoError(event:IOErrorEvent):void {
350
                                clearUpload();
351
                                var evt:IOErrorEvent = new IOErrorEvent("uploadIoError", false, false, event.text);
352
                                dispatchEvent(evt);
353
                        }
354
 
355
                        // Called on upload security error
356
                        private function onUploadSecurityError(event:SecurityErrorEvent):void {
357
                                clearUpload();
358
                                var evt:SecurityErrorEvent = new SecurityErrorEvent("uploadSecurityError", false, false, event.text);
359
                                dispatchEvent(evt);
360
                        }
361
 
362
                        // Change view state
363
                        private function changeView():void {
364
                                currentState = (currentState == "mini") ? "" : "mini";
365
                        }
366
 
367
                ]]>
368
        
369
 
370
        
371
                
372
                        
373
                        
374
                        
375
                
376
        
377
 
378
        
379
                
380
                        
381
                
382
        
383
 
384
    
388
 
389
        
390
        
391
                
392
                        allowMultipleSelection="true" verticalScrollPolicy="on"
393
                        draggableColumns="false" resizableColumns="false" sortableColumns="false">
394
                        
395
                                
396
                                
397
                        
398
                
399
        
400
        
401
                
402
                
403
                
404
                
405
        
406
            id="btnUpload" click="startUpload()" icon="@Embed('assets/bullet_go.png')" visible="{_showSendButton}"/>
407
        
408