10 Replies Latest reply on Jun 25, 2010 7:59 AM by Francisc

    Void FileReference variable

    Francisc Level 3



      I want to void a FileReference Class variable.

      Problem is that if I do this:



      fileReference=new FileReference();


      The variable loses all listeners and I want it to keep them so I can reuse it to upload pictures.


      How can I empty it after a file is selected but still keep it's listeners so that in the case of another file being selected it still does the same thing?


      Thank you!

        • 1. Re: Void FileReference variable
          David_F57 Level 5



          Do you want to use filereference or filereferencelist ?



          • 2. Re: Void FileReference variable
            Francisc Level 3

            I am using FileReference for a single file.

            I want to reuse it because after previewing the selected image, the user can change his mind and select another and another etc.


            After submitting and file upload, I want to reset the form to be reusable again.


            If I set fileReference=null, all listeners are lost. I have to reinstantiate them all over again.


            Is there another way of doing that without removing all listeners then adding them again?

            • 3. Re: Void FileReference variable
              David_F57 Level 5



              If you declare your filereference and listeners outside of the form there should be no need to reset as each time you browse the reference will be updated, below is the most basic use of filereference and that should work even if you have a child form.




              <?xml version="1.0" encoding="utf-8"?>

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"


                 xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)">


              <!-- Place non-visual elements (e.g., services, value objects) here -->




              import mx.events.FlexEvent;


              private var fi:FileReference = new FileReference();

              private var loader:Loader = new Loader();

              private var imagesFilter:FileFilter = new FileFilter("Images", "*.jpg;*.gif;*.png");

              private var ba:ByteArray = new ByteArray();



              protected function application1_creationCompleteHandler(event:FlexEvent):void






              private function onComplete(event:Event):void


              img.source = fi.data;



              private function onSelect(event:Event):void






              protected function button1_clickHandler(event:MouseEvent):void







              <mx:Image id="img" x="146" y="170" width="263" height="236"/>

              <s:Button x="232" y="111" label="Button" click="button1_clickHandler(event)"/>


              1 person found this helpful
              • 4. Re: Void FileReference variable
                Francisc Level 3

                That's exactly what I am doing.


                However, I want to `reset` the form so that validation fails if no file is selected.


                File selection is mandatory and after a successful upload, if he wants to upload another (without refreshing), if I don't somehow void the fileReference, he won't be promted to select a new file as there is already on in the fileReference variable.


                So basically, I want to have a null fileReference variable so that I can check if he selected a new file but still keep all the listeners.


                The shorthand would be:






                I was hoping there would be a more elegant way for this though.



                Thank you for your answer!

                • 5. Re: Void FileReference variable
                  David_F57 Level 5



                  Could you just disable the form or upload 'button' or empty the filename from the form after a successful transfer then re-enable it in the browse for file function...



                  • 6. Re: Void FileReference variable
                    Francisc Level 3

                    I am doing that to make sure the button doesn't get pressed many times, but that still doesn't empty the fileReference variable.

                    fileReference.upload(etc) still for the previously selected fine.

                    • 7. Re: Void FileReference variable
                      John Hall Level 4

                      Well, I've been going through the same thing the last couple of days. I was waiting for harUI to answer because my solution was less than elegant. However, I'll share it since he has chimed in.


                      Right before I go get a file, I call a function called reInitFileReference:


                      private function reInitFileReference():void{
                                      fileReference.removeEventListener( IOErrorEvent.IO_ERROR,ioErrorHandler );
                                      fileReference.removeEventListener( DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
                                      fileReference.removeEventListener( Event.COMPLETE, loadFileInfo);
                                      fileReference = new FileReference();
                                      fileReference.addEventListener( IOErrorEvent.IO_ERROR, ioErrorHandler,false,0,true );
                                      fileReference.addEventListener( DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler, false,0,true );
                                      fileReference.addEventListener( Event.COMPLETE, loadFileInfo,false,0,true );


                      and make sure I use weak references when I create the listeners.

                      • 8. Re: Void FileReference variable
                        Francisc Level 3

                        I know about that, problem is, like you say, it's not elegant.


                        • 9. Re: Void FileReference variable
                          John Hall Level 4

                          But if we keep making little comments like this, at least we can keep the question at the top of the list and maybe Greg or Flex harUI will chime in ;-)

                          • 10. Re: Void FileReference variable
                            Francisc Level 3

                            Yeah, sorry, it came out wrong, I didn't mean "not elegant" in a dismissive way.