3 Replies Latest reply on Apr 1, 2010 10:16 AM by wbnc1902

    Event.COMPLETE and File.upload

    wbnc1902 Level 1

      Hello fellow flexers,

           I am working on a way to upload between 1 and 100+ files using a flex application.  I have code that works really well which uploads the files without too much problem.  The problem I am having is letting the user know that the file upload is complete.  I want to show an alert that lets the user know that ALL the files have been uploaded (and potentially do some behind the scenes file management duties).  Well the problem is I cannot seem to get the event.complete handler to fire when the LAST file has been uploaded.  It seems to fire at any point during the upload.

       

      The following is a code example:

       

      public function upload(event:MouseEvent):void
                {
                     var params:URLVariables = new URLVariables();
                         params.cn = countyName.selectedLabel; 
                         params.eID = electionDate.selectedItem.id;
                              params.equip = equipmentType.selectedLabel;
                         params.loc = loc.text;                       
                     var fileList:Array = fileReferenceList.fileList;
                     for (var i:Number=0; i<fileList.length; i++)
                     {
                          var request:URLRequest = new URLRequest("http://[mydomain]/uploader.cfm");                                             
                          request.method = URLRequestMethod.POST;                    
                          request.data = params;                    
                          var file:FileReference = FileReference(fileList[i]);
                          file.upload(request);
                          
                     }
                     file.addEventListener(Event.COMPLETE, displayAlert);
                }
      

       

      As you can see from the code the "file.addEventListener" is not added until after the For loop has completed.  However, this does not mean that the files have actually uploaded to the server.  If I add the eventListener inside the loop I will get anywhere from 1 to 100+ alerts showing the files have uploaded correctly.

       

      Now I have no problem letting it upload one at a time (if needed), but I am having trouble figuring out how to control that.

       

      Suggestions?

       

      Thanks!

        • 1. Re: Event.COMPLETE and File.upload
          wbnc1902 Level 1

          Bump...I hope to get this going soon.

           

          I appreciate any thoughts / assistance.

          • 2. Re: Event.COMPLETE and File.upload
            Matt Le Fevre Level 4

            Now I have no problem letting it upload one at a time (if needed), but I am having trouble figuring out how to control that.

             

             

            remove the event after it's been completed, eg:

             

            private function displayAlert(e:Event):void
            {
                   file.removeEventListener(Event.COMPLETE, displayAlert)
            }
                          

             

            and then add it again when the next file is uploading, this'll control that aspect.

             

             

            For finding out when all files have been uploaded, as your event is now controlled, simply increment an integer value every time the event is completed.

             

            Compare that to the amount of files that originally planned to be uploaded, when the values match then you know that all the files have now been uploaded, and you output an alert message informing the user that it's now complete.

             

            you could also link the integer value to a progress bar, which'll display the files upload visually.

            • 3. Re: Event.COMPLETE and File.upload
              wbnc1902 Level 1

              I am not following you.  I can see how that would work if I could hold the upload of the files until the last one completes.  The example you provided doesn't do that?  Also I get a flex error if I try and remove the event listener from the displayAlert function.  The following code is what I have to upload files.  I have added an event listener and a few public variables that 'should' keep track of the iterations in the for loop.  It also sets a variable to the total number of files being uploaded.

               

              private function upload(event:MouseEvent):void
                        {
                             var params:URLVariables = new URLVariables();
                                 params.cn = countyName.selectedLabel;
                                 params.eID = electionDate.selectedItem.id;
                                 params.equip = equipmentType.selectedLabel;
                                 params.loc = loc.text;                       
                             var fileList:Array = fileReferenceList.fileList;
                             for (var i:Number=0; i<fileList.length; i++)
                             {
                                  var request:URLRequest = new URLRequest("http://[mydomain]/ballotUploader.cfm");                                             
                                  request.method = URLRequestMethod.POST;                    
                                  request.data = params;                    
                                  var file:FileReference = FileReference(fileList[i]);
                                  cntr = i; //This should keep track of the iterations
                                  completeCntr = fileList.length - 1;     //This value holds the total number of files being uploaded               
                                  file.upload(request);
                                  file.addEventListener(Event.COMPLETE, displayAlert);               
                                  
                             }
                             
                        }
              

               

               

              The following is an example of the displayAlert function:

               

              private function displayAlert(evt:Event):void
                        {
                             file.removeEventListener(Event.COMPLETE, displayAlert); //<--- this causes an eror: "Access to undefined property file"
                             if (cntr == completeCntr)//if cntr equals completeCntr "all" files have been uploaded 
                             {
                                  Alert.show('All files have been uploaded','Sucess!',mx.controls.Alert.OK);     
                             }                
                             
                        }
              

               

               

              The problem I have is it loops through the for loop so quickly that it displays the "All files have been uploaded" before they all have.  This is because all the for loop is doing is opening up a connection to the cfm script, once that happens it starts again for the next file, and the next file...etc.  It sounded like in your post that I could control the for loop to wait until one file uploads, once that is complete start again.  How do I do that?

               

              Thank you so much for your help!

               

              Will