PDA

View Full Version : import a csv document in to flash



samuk
February 26th, 2008, 04:19 PM
Hi, is it possible to import a csv document in to flash?

I want to access a csv document that is on a server every hour. I want to pull out the data and assign it to variables. I would like to run an animation using these variables.

Is this possible? Any help and examples is much appreciated. I have experience of using flash but i am a newbie in actionscript.

Many Thanks

Sam

bcswebstudio
August 14th, 2008, 11:55 PM
I recall an old Developers Journal article that outlined it.. Found it online here:
http://flex.sys-con.com/node/45619
unfortunately, the full source is no longer linked. Good luck-- post back here if you get it licked.

-bronius

bcswebstudio
August 15th, 2008, 12:01 AM
Update:
taken from:
http://gemsres.com/story/45619/source.html
here's the source accompanying the article linked above on parsing csv for mx 2004, but do visit the page above for a single zip containing .as class file, .fla source, and .csv sample data file:

Code I - parsing the CSV file


public function parseCSV(rawData:String):Array {
var ii:Number, columnArray:Array, rowObject:Object;
var returnArray:Array = new Array();
var rowDelimiter:String = (rawData.indexOf('\r\n') > -1) ? '\r\n' :
(rawData.indexOf('\r') > -1) ? '\r' : '\n';
var columnDelimiter:String = qualifier + ',' + qualifier;
var rowsArray:Array = rawData.split(rowDelimiter);
if(!columns.length) {
columns = removeQualifier(rowsArray.shift().toString(),
qualifier).split(columnDelimiter);
}
for(var i:Number = 0; i < rowsArray.length; i++) {
columnArray = removeQualifier(rowsArray[i].toString(),
qualifier).split(columnDelim);
if(columnArray.length == columns.length) {
rowObject = new Object();
for(ii = (columnArray.length - 1); ii >= 0; ii--) {
rowObject[columns[ii]] = columnArray[ii];
}
returnArray.push(rowObject);
}
}
return returnArray;
}
Code II - stripping the qualifier surrounding field data


private function removeQualifier(originalString:String, qualifier:String):String {
var modifiedString = originalString;
if(modifiedString.charAt(0) == qualifier) {
modifiedString = modifiedString.substring(1);
}
if(modifiedString.charAt(modifiedString.length - 1) == qualifier) {
modifiedString = modifiedString.substring(0, (modifiedString.length - 1));
}
return modifiedString;
}
I hope this is not a violation of copyright... It came straight out of that great article, but was not immediately obvious how to view the source which would have been otherwise freely available.

andyman101
April 17th, 2012, 12:11 AM
is there an fla example file for this?
:)
thanks