RETRIEVING DATA FROM A TEXT FILE

  
This tutorial explains how a Flash movie can retrieve data from an external text file.   To understand this tutorial, it's recommended that you are already familiar with: 
  • how to access variables in Flash
  • Flash Action Scripting

Flash 4 (and newer) has a capability to retrieve the content of a text file by using the LoadVariables function.  This is quite powerful, for example, you can create a news engine without manually editing the FLA file every time the news changes.  

REQUIREMENTS

The process of reading the file is relatively straightforward and simple, but there are some details to be aware of.

  1. The text file must be in the standard ASCII format (text-plain). This means that you cannot use Word, html, or any format other than plain-text.  Flash would not understand embedded formatting such as found in Word, or excel files. 
  2. The text file must contain variables in CGI-encoded (MIME) format.  Basically, this means 2 things.  First, you need to pass the data as variable(s) - more on this below.  Second, there are special characters that need to be represented differently (encoded), for example <SPACE> should be represented with "+" or "%20", although Flash 4 does not seem to enforce this (see text file example below).
  3. It might take a while to transfer the data. So if the Flash movie is supposed to further processing with the data, make sure to check whether the data has been received or not. 

More information about item 2 and 3 can be read here.

EXAMPLE

As an example, this is a text file, it's called text.txt (this name is just an example, you can use any valid file-name, the extension does not have to be ".txt", but it has to contain plain-text).  text.txt

There are 3 variables in this file:

  • header
  • theText
  • done

All the variable names are followed by a "=" sign, and an "&" sign acts as separator between variableName=variableValue pairs.  These are part of the MIME requirements mentioned in item 2 above.  Please be sure that you can spot those variables before continuing.   (Note that there can't be any <SPACE> on a variable name.)

Again, notice how the & and = communicate special meanings.  What if I need to use  "&" and "=" character as part of the data?  This is where you most likely need to use MIME encoding.  In that case, you need to replace the special character with a % sign, followed by the character's ASCII-value.  

For example, notice, the variable header  has %20, which represent a <SPACE>.  Also, notice that the theText variable contains %26 .... which doesn't quite make sense to be there, but that number actually represents a & character.  Those numbers are ASCII numbers (hexadecimal code of the character).  If you need to know the ASCII-value for a particular character, consult an ASCII table; this is a standard programmer reference that should be easy to find on the web.

Now, back to the variables.   For the sake of this example, I will be using those variables to create a news site, so that: 

  • header contains the title of the news
  • theText contains the content of the news
  • done is a flag that can be used to check whether the data has been transferred or not. This kind of flag should be placed at the end of the data.   The reason is because the data transfer will be sequential, and if this flag is being sent elsewhere other than at the end of the data stream, the Flash movie might mistakenly think the whole file has been transferred, when actually only part of it has.  (You do not need to have any flag if the Flash movie is not going need the data to do some processing.)

These variable names are arbitrary, but don't use <space> or any special character as part of a variable name (another MIME requirement).  

To read the text file, I created a very simple Flash movie.  The movie is shown below.   You should see the content of the the text file (or the news, if you will) there.  

The Flash Movie (a fake news applet) 

 

Side note: If you are still perplexed by the MIME business, here's more... Notice how Flash converted the %20 and the + sign in the header variable to <SPACE> character.  Also notice that %26 is converted into a & character.   (Compare the content of the text file with what is displayed in the movie.)

This Flash movie uses LoadVariables to "read" the text file, as shown in the picture below.  When this LoadVariables is executed, the text file will be read.  Once the data transfer is complete (i.e.: the text file is completely read), then the Flash movie will have 3 variables: header, theText, and done.  Note: It is not necessary to manually create these variables in the Flash movie; Flash will create them automatically - if they already exists, the values will be overwritten.)   

Flash 4:


Flash 5:


(If you don't quite understand the parameters of LoadVariables, an explanation can be found on this other  tutorial.  Using Send using GET or Send using POST prevents the data from being cached by the browser.)  Caching problem can be a serious issue, please see Caching Problem discussion.

To display the data, I purposefully created two editable text-fields with the Variable Property set to the same variable names as in the text file.  

Specifically, the first text field has the Variable property set to header (shown below); and the next text field is set to theText.  Remember that Flash will automatically update a text field with the specified variable, so this really is a quick and convenient way to display the content of the text file.   

Flash 4:
The crosshatched rectangle highlights a editable text field, named "header."  

Flash 5:
The blue rectangle highlights a Dynamix Text field, named "header."  


The "header" text field will contain the string "Vacation Is Good" once the text file is loaded.  Flash knows what value to put there because the string "Vacation Is Good" is assigned to variable "header"  in the text file.

Notes:

  • You can load more than one text file on a single Flash movie.

  • To test the Movie, you must use "Publish Preview->html," using the "Test Movie" menu will not work.

  • You can now download Flash 4 and Flash 5 FLA files here:
    Flash 4

  • Flash 5
    Test text file

<<INDEX>>

March 2000
(C) F. Permadi


permadi@permadi.com