This uses the FileReference class, and shows how to access a text file, and save a text file.
To access the text file:
var fr:FileReference = new FileReference(); //if this is created within a function, it won't work
//because it really only exists within that function
function lFile(evnt:MouseEvent):void {//this function has to be initiated by the user
fr.addEventListener(Event.SELECT, seld); //the method that will be called when the user selects a file.
fr.browse(); //this opens up the dialog box
}
function seld(evnt:Event):void {
fr.addEventListener(Event.COMPLETE, compd);
fr.load(); //makes the file available to the swf
}
function compd(evnt:Event):void {
outpt.text = String(fr.data);
}
The uploading of a file has to be initiated by the user, e.g. by clicking a button.
To save some text to a file:
var fr2:FileReference = new FileReference();
function sFile(evnt:MouseEvent):void {//again, this has to be called by the user
fr2.save(outpt.text, 'textfile.txt');//second parameter is optional
}
One odd thing I noticed with this, by specifying what I wanted the text file to be called, if I accepted that name in the saveas dialog box, then it was fine, even though the '.txt' was not shown in the dialog box. If, however I changed the name to something else, then I had to add the '.txt' myself, or it wouldn't know what kind of file it was supposed to be. (So if I go to Windows Explorer, it doesn't have the right icon, and when I try to open it from there, I get asked what program should be used.)
No comments:
Post a Comment
Please enter your message here...