1 Reply Latest reply on Nov 2, 2009 4:33 PM by injpix

    How can I use FileStream and AsyncToken together?

    injpix Level 3

      I am trying to use an AsyncToken with FileStream.  I know how to use a token with HTTPService, since it's send method simply returns an AsyncToken type.  But I cant find a way to have it work with FileStream.

       

      Thanks.

        • 1. Re: How can I use FileStream and AsyncToken together?
          injpix Level 3

          Ok, I found a way.  The code is gathered from a few classes from my project, so it may seem like an overkill.

           

          public function init():void
          {
               // call the service
               var token:AsyncToken = loadFile( url );
               
               //responder is an instance of another class that implements IResponder, which has a result and fault method.
               token.addResponder( this.responder );
               token.addEventListener(ResultEvent.RESULT, responder.result );
          }
          
          public function loadFile( url:String ):AsyncToken
          {
               token = new AsyncToken();
             
               var file:File = url;
               fileStream = new FileStream();
          
          
               fileStream.addEventListener(Event.COMPLETE, fileStreamResult );
               fileStream.openAsync(file, FileMode.READ);       
             
               return token;           
          }
          
          
          public function fileStreamResult(event:Event):void
          {
               var xmlString:String = fileStream.readMultiByte(fileStream.bytesAvailable, File.systemCharset);
               fileStream.close();
             
          
                  //create a new event and with the new data
               token.dispatchEvent( ResultEvent.createEvent(xmlString) );
          }