Loading external data
===================================================================
Pulling data from a local file using loadVars
Create a text file in notepad, and type something in it. Name it 'wah.txt' and place it in the same directory as your fla
Create a dynamic text box with the var: RADISH. This will hold the loaded data.
loadVars in its simplest form - add this code to the first frame of your timeline:
PIE=new LoadVars();
PIE.onData=function(raw){
RADISH=raw;
}
This will load the contents of your text file and display them in the textbox. To load the contents using a button, add this to your button:
on(press){_root.PIE.load("wah.txt");}
You can manipulate RADISH as you would any other string, once the load has finished.
===================================================================
Pulling data from a remote file using loadVars
NOTE: Some of these codes will NOT work when you preview the swf by pressing CTRL+ENTER.
If you get a 'Error opening URL' or 'undefined' values, publish the swf and then view it in the standalone player or a browser.
The limitations of accessing only local files are obvious. To achieve the full potential, you'll probably want to access a data file on the net.
- To access a remote file from your PC, just change the file path in the button actions:
on(press){_root.PIE.load("http://Domain_A.com/subfolder/wah.txt");}
- If your swf is uploaded and the "wah.txt" file is on the same server, use a relative path as you would on your machine
on(press){_root.PIE.load(../subfolder/wah.txt");} or on(press){_root.PIE.load("wah.txt");}
- NOW if your swf is uploaded (to Domain_A), but your data file is on a different domain (Domain_B), we hit complications (AKA Flash's Security Sandbox)
Firstly, you'll need to use an absolute path on your button. Then you'll need a System.security.allowDomain command.
on(press){
System.security.allowDomain("Domain_B.com")
_root.PIE.load("http://Domain_B.com/subfolder/wah.txt");
}
...but wait, it still doesn't work!!! WTF!?!?! (well, those were the sort of words I used when I first tried this)
So, apparently, we need a crossdomain file on Domain_B (the one that hosts 'wah.txt'). Open Notepad, and paste this in:
<?xml version="1.0"?>
<!-- http://www.Domain_B.com/crossdomain.xml -->
<cross-domain-policy>
<allow-access-from domain="*.Domain_A.com" />
</cross-domain-policy>
Save this file as crossdomain.xml (it must be named this), and upload it to the ROOT of Domain_B.
BINGO! Now you can host a swf on NG, and pull data (php, html, text, asp, etc) from your own site.
===================================================================
I'm aware that many people will want to send data as well as recieve it. Since I'm trying to keep the AS: threads easily readable, that will be covered in a separate topic (when I or someone else gets around to it).
Associated reading:
http://www.sitepoint.com/forums/showthread.php?t=120122&mode=linear
http://www.actionscript.org/forums/archive/index.php3/t-58351.html
http://livedocs.macromedia.com...MX_2004&file=00001418.html
http://www.moock.org/asdg/technotes/crossDomainPolicyFiles/