Reading a file Asynchronously with ActionScript 3

Written on May 31, 2007 – 9:42 am | by John |

Sometimes (okay, manytimes) files are large enough that the process of opening and reading its contents may take some time. To solve issues related to the extra time this may take AS3 allows you to open files asynchronously. I'd like to add to the information from the reading files with AS3 post. Here we'll cover the code necessary to open a file asynchronously and read the file contents as they become available.

Because we are now doing something asynchronously, we will be dealing with events. As the file contents are being read The FileStream object broadcasts progress events that we can listen for and respond to. This ensures that when we are reading data when the data is available.

So, onto the code:

Actionscript:
  1. // Imports
  2. import flash.filesystem.FileMode;
  3. import flash.filesystem.FileStream;
  4. import flash.filesystem.File;
  5. import flash.events.ProgressEvent;
  6. import flash.events.Event;
  7.  
  8. // Declare the FileStream and String variables
  9. private var _fileStream:FileStream;
  10. private var _fileContents:String;
  11.  
  12. private function onCreationComplete():void // Fired when the application has been created
  13. {
  14. var myFile:File = File.appResourceDirectory; // Create out file object and tell our File Object where to look for the file
  15. myFile = myFile.resolve("mySampleFile.txt"); // Point it to an actual file
  16.  
  17. _fileStream = new FileStream(); // Create our file stream
  18.  
  19. _fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress); // Add our the progress event listener
  20. _fileStream.addEventListener(Event.COMPLETE, onFileComplete); // Add our the complete event listener
  21.  
  22. _fileStream.openAsync(myFile, FileMode.READ); // Call the openAsync() method instead of open()
  23. }
  24.  
  25. private function onFileProgress(p_evt:ProgressEvent):void // Event handler for the PROGRESS Event
  26. {
  27. _fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, "iso-8859-1"); // Read the contens of the file and add to the contents variable
  28.  
  29. fileContents_txt.text = _fileContents; // Display the contents. I've created a TextArea on the stage for display
  30. }
  31.  
  32. private function onFileComplete(p_evt:Event):void // Event handler for the COMPLETE event
  33. {
  34. _fileStream.close(); // Clean up and close the file stream
  35. }

As for what all that code does - first off, import the required classes:

Actionscript:
  1. import flash.filesystem.FileMode;
  2. import flash.filesystem.FileStream;
  3. import flash.filesystem.File;
  4. import flash.events.ProgressEvent;
  5. import flash.events.Event;

Then we'll need to create a couple variables. First, the FileStream which we'll use to read our file and second, a String variable that we'll use to display the file contents.

Actionscript:
  1. private var _fileStream:FileStream;
  2. private var _fileContents:String;

Now we'll create an onCreationComplete() method. This method will handle the creationComplete event of the application to get things rolling. Inside the onCreationComplete() method we'll need to create out File object as well as the FileStream object.

Actionscript:
  1. private function onCreationComplete():void // Fired when the application has been created
  2. {
  3. var myFile:File = File.appResourceDirectory;
  4. myFile = myFile.resolve("mySampleFile.txt");
  5.  
  6. _fileStream = new FileStream();
  7. }

Still in the onCreationComplete() method, we'll need to handle those events that will be dispatched while reading the file asynchronously. The events that we will handle are:

  • ProgressEvent.PROGRESS - This will fire as the bytes are read from the file into the buffer.
  • Event.COMPLETE - This will fire when all the bytes of the file have been read into the buffer.
Actionscript:
  1. private function onCreationComplete():void // Fired when the application has been created
  2. {
  3. var myFile:File = File.appResourceDirectory;
  4. myFile = myFile.resolve("mySampleFile.txt");
  5.  
  6. _fileStream = new FileStream();
  7.  
  8. _fileStream.addEventListener(ProgressEvent.PROGRESS, onFileProgress)
  9. _fileStream.addEventListener(Event.COMPLETE, onFileComplete);
  10. }

The final part of the onCreationComplete() method is to open the file asynchronously. Add the following line to the end of the method:

Actionscript:
  1. _fileStream.openAsync(myFile, FileMode.READ);

Now to handle those events and read our file. First, we'll deal with the PROGRESS event. Inside the onFileProgress() event handler method, we read the availableBytes from the file and add it to our String variable. Notice the second parameter of the readMultiByte() method - "iso-859-1". This specifies the content type of the file that we are reading. I'll cover some of the other content types in a later post. So, keep an eye out for that one if you're curious. Finally, we update the TextArea, to show we are actually reading the file.

Actionscript:
  1. private function onFileProgress(p_evt:ProgressEvent):void
  2. {
  3. _fileContents += _fileStream.readMultiByte(_fileStream.bytesAvailable, "iso-8859-1");
  4.  
  5. fileContents_txt.text = _fileContents;
  6. }

We still need to clean up after ourselves, so in the onFileComplete() method, we'll need to close out FileStream object.

Actionscript:
  1. private function onFileComplete(p_evt:Event):void
  2. {
  3. _fileStream.close();
  4. }

A very simple example, and it is still only text that we are reading. Next time I'd like to cover how to read different file types. So, like I said, keep an eye our for that post.

Share and Enjoy:
  • Digg
  • del.icio.us
  • Netvouz
  • description
  • ThisNext
  • MisterWong
  • Wists
  • Facebook
  • StumbleUpon
  • Technorati
  • YahooMyWeb

Related Posts

Put your related posts code here
Find entries :

Want to subscribe?

  Subscribe in a reader or,
Subscribe via email:

About me

I'm a senior developer at RealEyes Media, Adobe Certified Instructor and Adobe Certified Professional. Here you'll get my ideas and experience Flex, Flash, ColdFusion and related technologies as well as some generally off the wall stuff.