The MIME Format


Data being loaded into Flash by means of loadVariables(), loadVariablesNum(), or the LoadVars class must be in standard MIME format. It is in application /x- www-form-urlencoded format and is basically a bunch of name/value pairs.

Following is a basic example of a name/value pair in MIME format:

 name=David 

Unlike ActionScript, here we do not put the value of the variable name in quotes. Also, there is no space after the equal sign; this is because if there were a space, the value would include the space, making "David" become " David".

The preceding example had only one name/value pair. To separate multiple name/value pairs, the ampersand (&) is used like this:

 name=David&age=25 

You can, of course, put ampersands on either side of the name/value pair just to make sure there are no errors, like this:

 &name=David&age=25& 

This way, you can clearly tell where each name/value pair begins and ends.

You can also create URL-encoded strings in Flash for special characters, such as the ampersand, using the escape() function.

The escape() and unescape() functions

Because an ampersand is used to separate individual name/value pairs in URL-encoded content, they cannot be within the content itself or Flash will read it as a separator, which can cause errors. Using the escape() function, you can encode the ampersand in a URL-encoded format, as in the following code:

 //create the string var myString:String = escape("title=War & Peace"); //send the encoded string to the output panel trace(myString); //output: title%3DWar%20%26%20Peace 

As you can see, the string being created is URL-encoded and stored in a variable. Then the string is sent to the Output panel, where you can see that the ampersand and the spaces in the string have been replaced with special characters. To get the string back to normal, use the unescape() function, which will convert URL-encoded strings back to their original format like this:

 //create the string var myString:String = escape("title=War & Peace"); //send the encoded string to the output panel trace(myString); trace(unescape(myString)); //output: title%3DWar%20%26%20Peace //        title=War & Peace 

Now that you have seen the basic format that the data we are going to load needs to be in, let's make a file to hold the information that we will be using in several examples throughout this chapter.

1.

Open a text editor such as Notepad or SciTe and put this code in it (you can replace my information with yours if you like):

 &name=David& &age=25& &location=Richmond& 

Notice that they are on separate lines. This is not necessary; it simply makes it easier to read. Each line begins and ends with an ampersand to make sure that none of the name/value pairs accidentally joins another.

2.

Save this file as sample.txt.

Now that you have a file you can work with, let's go over some of the methods used to bring in data from external sources, starting with the loadVariables() method.




Macromedia Flash Professional 8 Unleashed
Macromedia Flash Professional 8 Unleashed
ISBN: 0672327619
EAN: 2147483647
Year: 2005
Pages: 319

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net