Nov 12, 2007

CSV in Flash Lite

Recently I'd been working on an application that required Flash Lite to import data from a CSV file format.

CSV stands for "Comma Separated Value". It is a standard means of exchanging data, and virtually all spreadsheets and databases can import data presented in this fashion. This is what a CSV spreadsheet will look like when opened in Excel.

For Flash Lite to import data from a CSV file format, you can make use of a middle language - PHP. Written below is a basic code showing interaction between Flash Lite and PHP for reading CSV files.

PHP File

$file = $_POST['link'];
$row = 1;
$handle = fopen($file, "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
   $num = count($data);
   for ($c=0; $c < $num; $c++) {

The code above is accepting the CSV file URL from the 'link' variable sent from Flash, and tossing it into the 'fopen' command. It is then parsing the result using 'fgetcsv', and echoing the returned data value back into Flash.

Flash File

varSender = new LoadVars();
varReceiver = new LoadVars(); = "";
varSender.sendAndLoad(url, varReceiver, "POST");
varReceiver.onLoad = function(){

The Flash file is sending the 'link' variable to the PHP file and receiving the 'echoed' values from PHP in the 'varReceiver' Object. The received values can then be used within the application.

Flash Lite interaction with CSV can be very useful in reading data for several games and applications. You can use this code and explore the possibilities of adding much more interaction to your content.

Labels: ,


At 7:24 PM , Blogger Abdul said...

I assume, you are doing parsing on server-side to save some cpu cycles on client? Though CSV is not complex format and can be done on client-side as well (provided it doesn't have thousands of rows and cols) :)

Other thing I noticed, you are using two LoadVars instances. I think, you can get things done with one only..

Following should just work fine:-

varSender = new LoadVars(); = "";
varSender.sendAndLoad(url, varSender, "POST");
varSender.onLoad = function(){

At 7:25 PM , Blogger Abdul said...

Some more modification:-

varSender = new LoadVars(); = "";
varSender.sendAndLoad(url, varSender, "POST");
varSender.onLoad = function(){

At 7:29 PM , Blogger cisnky said...

I prefer using two loadVars objects. You get a clear separation of what is sent and what is received.

At 9:30 PM , Blogger Mariam Dholkawala said...

@abdul - The CSV code is a part of a much extensive script that the client would absolutely not be able to handle, so the server side :)

However would try your suggestion with LoadVars to suit this application.

@cisnky - I too am comfortable with keeping the objects separate for sending and receiving data..especially with regular updates within the application.

At 10:30 PM , Blogger Abdul said...

It's totally ok and legal to use two LoadVars, that's why it's method take another instance...

My response was more in context of performance, on one side you talking about devices and other side you are happy to create two instance of an Object (which would persist in memory as long as it's alive?)

To keep things clean (for development purpose), it's alright to have two different objects.

Different thoughts and perspectives, we all got different ways of doing things :)


Post a Comment

Subscribe to Post Comments [Atom]

<< Home