본문 바로가기

공부방/Flex

Flex 멀티 파일 업로드 예제

현재 하고 있는 프로젝트에서 멀티파일 업로드 기능을 구현하기 위해서 참고한 예제

FileReferenceList 사용이 포인트 였군ㅋ


    
    
        = 1) {
                    for (var k:Number = 0; k < _refAddFiles.fileList.length; k++) {
                        _arrUploadFiles.push({label:_refAddFiles.fileList[k].name, data:_refAddFiles.fileList[k]});
                    }
                    listFiles.dataProvider = _arrUploadFiles;
                    listFiles.selectedIndex = _arrUploadFiles.length - 1;
                }                
                if (arrFoundList.length >= 1) {
                    Alert.show("The file(s): \n\n• " + arrFoundList.join("\n• ") + "\n\n...are already on the upload list. Please change the filename(s) or pick a different file.", "File(s) already on list");
                }
                if (_arrUploadFiles.length == 0) {
                    btnUpload.enabled = false;
                } else {
                    btnUpload.enabled = true;
                }
            }
            
            
            // Cancel and clear eventlisteners on last upload
            private function clearUpload():void {
                _numCurrentUpload = 0;
                _refUploadFile.removeEventListener(ProgressEvent.PROGRESS, onUploadProgress);
                _refUploadFile.removeEventListener(Event.COMPLETE, onUploadComplete);
                _refUploadFile.removeEventListener(IOErrorEvent.IO_ERROR, onUploadIoError);
                _refUploadFile.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadSecurityError);
                _refUploadFile.cancel();
            }
            
            // Called to upload file based on current upload number
            private function startUpload(booIsFirst:Boolean):void {
                if (booIsFirst) {
                    _numCurrentUpload = 0;
                }
                if (_arrUploadFiles.length > 0) {
                    _winProgress = winProgress(PopUpManager.createPopUp(this, winProgress, true));
                    _winProgress.btnCancel.removeEventListener("click", onUploadCanceled);
                    _winProgress.btnCancel.addEventListener("click", onUploadCanceled);
                    _winProgress.title = "Uploading file to " + _strUploadDomain;
                    _winProgress.txtFile.text = _arrUploadFiles[_numCurrentUpload].label;
                    _winProgress.progBar.label = "0%";
                    PopUpManager.centerPopUp(_winProgress);
                    
                    // Variables to send along with upload
                    var sendVars:URLVariables = new URLVariables();
                    sendVars.action = "upload";
                    
                    var request:URLRequest = new URLRequest();
                    request.data = sendVars;
                    request.url = _strUploadScript;
                    request.method = URLRequestMethod.POST;
                    _refUploadFile = new FileReference();
                    _refUploadFile = _arrUploadFiles[_numCurrentUpload].data;
                    _refUploadFile.addEventListener(ProgressEvent.PROGRESS, onUploadProgress);
                       _refUploadFile.addEventListener(Event.COMPLETE, onUploadComplete);
                    _refUploadFile.addEventListener(IOErrorEvent.IO_ERROR, onUploadIoError);
                      _refUploadFile.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onUploadSecurityError);
                    _refUploadFile.upload(request, "file", false);
                }
            }
            
            // Called on upload cancel
            private function onUploadCanceled(event:Event):void {
                PopUpManager.removePopUp(_winProgress);
                _winProgress == null;
                _refUploadFile.cancel();
                clearUpload();
            }
            
            // Get upload progress
            private function onUploadProgress(event:ProgressEvent):void {
                var numPerc:Number = Math.round((Number(event.bytesLoaded) / Number(event.bytesTotal)) * 100);
                _winProgress.progBar.setProgress(numPerc, 100);
                _winProgress.progBar.label = numPerc + "%";
                _winProgress.progBar.validateNow();
                if (numPerc > 90) {
                    _winProgress.btnCancel.enabled = false;
                } else {
                    _winProgress.btnCancel.enabled = true;
                }
            }
            
            // Called on upload complete
            private function onUploadComplete(event:Event):void {
                _numCurrentUpload++;
                PopUpManager.removePopUp(_winProgress);
                if (_numCurrentUpload < _arrUploadFiles.length) {
                    startUpload(false);
                } else {
                    Alert.show("File(s) have been uploaded.", "Upload successful");
                }
            }
            
            // Called on upload io error
            private function onUploadIoError(event:IOErrorEvent):void {
                Alert.show("IO Error in uploading file.", "Error");
                PopUpManager.removePopUp(_winProgress);
                _winProgress == null;
                _refUploadFile.cancel();
                clearUpload();
            }
            
            // Called on upload security error
            private function onUploadSecurityError(event:SecurityErrorEvent):void {
                Alert.show("Security Error in uploading file.", "Error");
                PopUpManager.removePopUp(_winProgress);
                _winProgress == null;
                _refUploadFile.cancel();
                clearUpload();
            }
            
        ]]>
    
    
    
        
            
                
                
                    
                    
                
            
            
                
            
        
    



 

 

소스 보기

http://weblog.cahlan.com/files/file_uploads/srcview/index.html