1.21 gigawatts
1.21 gigawatts

Reputation: 17736

How to write files to a specific directory?

I'm trying to let the user select a directory and then write files to that directory.

I have this code that lets a user browse for a directory:

var file:flash.filesystem.File = new flash.filesystem.File();
file.browseForDirectory("Select a directory");
file.addEventListener(Event.SELECT, selectHandler);

protected function selectHandler(event:Event):void {
    // these contain the path where I want to save files to
    Object(fileReference).url;
    Object(fileReference).nativePath;
    // how do I create a file in that directory?
}

How do I create a file in the directory that the user selects?

Upvotes: 0

Views: 454

Answers (1)

Gurtej Singh
Gurtej Singh

Reputation: 3234

Your select handler code does not seem to be correct. You should get the reference of the Folder which is an object of the type File by doing event.currentTarget, and not Object or fileReference.

Next you can create a file using the FileStream class. Your selectHandler code should look like this:

protected function selectHandler(event:Event):void 
{
    var targetDirectory:File = event.currentTarget as File;
    var file:File = targetDirectory.resolvePath("htmlFile.html");
    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.WRITE);
    stream.writeUTFBytes("Any Text you want to create");
    stream.close();
}

Will work in AIR projects.

Hope this answers your question.

Upvotes: 3

Related Questions