3 Replies Latest reply on Jan 15, 2010 12:39 AM by Sebastien V.

    Can't add eventListener to fileReference

    vital_101

      Hey everyone,

       

      I'm trying to simply add an eventListener to a fileReference, yet Flex keeps telling me: "Access of undefined property fileRef"  and "Access of undefined property selectHandler".

       

      Here's the code.

       

      import flash.net.FileReference;

       

      var fileRef:FileReference = new FileReference();
      fileRef.addEventListener(Event.SELECT, selectHandler);
                     
      private function selectHandler(event:Event):void {
           mx.controls.Alert.show("hello!");
      }

       

       

      Any help would be greatly appreciated.

        • 1. Re: Can't add eventListener to fileReference
          Sebastien V. Level 3

          The call

           

          fileRef.addEventListener(Event.SELECT, selectHandler);
          

           

          needs to be in a method, you can't just drop it in the middle of the class you're using.


          Use the constructor for an AS3 class or the creationComplete event in a mxml file.

          • 2. Re: Can't add eventListener to fileReference
            vital_101 Level 1

            Going with what you said about the fileReference creation needing to be in a method, I dropped it in the 'browseFile' method and I didn't get errors this time.  However, after I select the file the 'selectHandler' method isn't launched.  The proper use of the selectHandler would be to start the upload of the file to the server no?  So shouldn't a simple alert box pop up without any problem?

             

            [CODE]

            <mx:Script>
                        <![CDATA[
                            import flash.net.FileReference;
                           
                            //var fileTypes:Array = new Array();
                            //var imageTypes:Object = new Object();

             

                            private function selectHandler(event:Event):void {
                                mx.controls.Alert.show("hello!");
                            }
                           
                            public function browseFile():void {
                                var fileRef:FileReference = new FileReference();
                                fileRef.addEventListener(Event.SELECT, selectHandler);
                                fileRef.browse();
                            }

             

                           
                        ]]>
                    </mx:Script>
                    <!-- Start Image Upload Stuff -->
                    <!-- On click, execute actionscript for file upload dialoug -->
                    <mx:FormItem label="Upload Image" fontWeight="bold" fontSize="11" width="402">
                        <mx:Button click="browseFile()" label="Browse" fillAlphas="[1.0, 1.0]" fillColors="[#FBFBFB, #C8C7C7]" width="100" themeColor="#638DA8" color="#4E585A" fontWeight="normal"/>
                        .............

             

            [/CODE]

            • 3. Re: Can't add eventListener to fileReference
              Sebastien V. Level 3

              Try to change to scope of your fileRef to a class variable, instead of a method internal variable.

               

              Code :

               

              <mx:Script>
                          <![CDATA[
                              import flash.net.FileReference;
              
                              var fileRef:FileReference = new FileReference();
                              private function selectHandler(event:Event):void {
                                  mx.controls.Alert.show("hello!");
                              }
                              
                              public function browseFile():void {
                                  fileRef = new FileReference();
                                  fileRef.addEventListener(Event.SELECT, selectHandler);
                                  fileRef.browse();
                              }
              
                        ]]>
               </mx:Script>
              
              etc...