2 Replies Latest reply on Dec 19, 2007 7:04 AM by ahess24

    FileReference

    ahess24
      When using the FileReference.browse function I never get a SELECT or CANCEL event dispatched. What am I doing wrong.

      Stripped down code:
      ------------------------------------------------------------------------------------------ -------------------------------------------------------------------------
      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" horizontalAlign="center">

      <mx:Script>
      <![CDATA[
      import flash.net.FileReference;
      import flash.events.Event;

      private function showBrowse() : void
      {
      var fileRef : FileReference = new FileReference();
      fileRef.addEventListener( Event.SELECT, selectHandler );
      fileRef.addEventListener( Event.CANCEL, cancelHandler );
      fileRef.browse();
      }

      private function selectHandler( event:Event ) : void
      {
      trace( "SELECT: " + event );
      }

      private function cancelHandler( event:Event ) : void
      {
      trace( "CANCEL: " + event );
      }
      ]]>
      </mx:Script>

      <mx:Button id="btnBrowse" label="Browse..." click="showBrowse()" />

      </mx:Application>
      ------------------------------------------------------------------------------------------ -------------------------------------------------------------------------

      When I click btnBrowse I get the file browse dialog but if I select a file and click open or click cancel I don't get any trace output for either function. I've place debug breaks on both listener functions and it never breaks on either point.
        • 1. Re: FileReference
          Gregory Lafrance Level 6
          I think this is happening because you are creating the FileReference object in showBrowse, and then listening for events after it returns. This code works:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
          <mx:Script>
          <![CDATA[
          import flash.net.FileReference;
          import flash.events.Event;

          private var fileRef:FileReference = new FileReference();

          private function init() : void {
          fileRef.addEventListener( Event.SELECT, selectHandler );
          fileRef.addEventListener( Event.CANCEL, cancelHandler );
          }

          private function showBrowse() : void {
          fileRef.browse();
          }

          private function selectHandler( event:Event ) : void {
          trace( "SELECT: " + event );
          }

          private function cancelHandler( event:Event ) : void{
          trace( "CANCEL: " + event );
          }
          ]]>
          </mx:Script>
          <mx:Button id="btnBrowse" label="Browse..." click="showBrowse()" />
          </mx:Application>
          • 2. Re: FileReference
            ahess24 Level 1
            WOW, of course. That was very n00b of me. Thanks for bringing me back on track.