0

I have looked far and wide but I can't find any resources on how to write to CSV files from flash AS3. I know that flash cannot write to it alone. I have used PHP before to write to a txt file but now I need to open a csv and insert/edit entries that are already in it.

How can I do this?

tshepang
  • 12,111
  • 21
  • 91
  • 136
Nebula
  • 679
  • 2
  • 17
  • 37

1 Answers1

5

There is no built in support for CSV, although it can be done manually:

//dummy data
var data:Array = [];
for(var i:int = 0 ; i < 100 ; i++) data[i] = {a:Math.random() * 100,b:Math.random() * 100,c:Math.random()};
//make a csv string
var csv:String = '';
for(i = 0 ; i < 100 ; i++) csv += data[i].a + ',' + data[i].b + ',' + data[i].c+'\n';
//write to disk
stage.doubleClickEnabled = true;
stage.addEventListener(MouseEvent.DOUBLE_CLICK,writeCSV);
function writeCSV(event:MouseEvent) {
    var file:FileReference = new FileReference();
    var bytes:ByteArray = new ByteArray();
    bytes.writeUTFBytes(csv);
    file.save(bytes,'test.csv');
}

Still, there is an as3 csv library out there.

I need to open a csv and insert/edit entries that are already in it.

Have a look at Data functions and manipulations on the csvlib wiki.

HTH

George Profenza
  • 50,687
  • 19
  • 144
  • 218
  • Thanks this works but I don't want it to be done manually. I used PHP instead. I posted the variables to the php code and searched the CSV file from there and inserted the data appropriately :) – Nebula Oct 26 '11 at 10:33