Skip to main content
August 7, 2011
Answered

Overwrite files automatically?

  • August 7, 2011
  • 2 replies
  • 2569 views

Hi

I'm writing an Air app that creates and writes an HTML page to the user's hard drive.  If the file already exists, it should be overwritten.

I was wondering if there is a way via AS3 to stop the prompt that asks if the users wants the file overwritten, and just automatically do it each time?

The Air program takes the contents of a text box (which contains the HTML page code) and then writes page.php to the hard drive.  The code I use is this:

docsDir = File.documentsDirectory.resolvePath( "page.php" );

docsDir.browseForSave("Save As");
docsDir.addEventListener(Event.SELECT, saveData);

function saveData(event:Event):void

{
    var newFile:File = event.target as File;
    var str:String = outputBox.text;
    var stream:FileStream = new FileStream();
    stream.open(newFile, FileMode.WRITE);
    stream.writeUTFBytes(str);
    stream.close();
}

Is it something simple, or can it not be done due to security restrictions?

This topic has been closed for replies.
Correct answer Kenneth Kawamoto

If you just open a file and write it AIR does not prompt you - there's nothing to prompt.

The reason you get the prompt is you are calling browse. There's no need for that, unless you want the user to choose the file name/location.

For example the following will create a text file on desktop without any prompts whatsoever:

var file:File = File.desktopDirectory.resolvePath("test.txt");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.UPDATE);
stream.writeUTF("this is a test");
stream.close();

2 replies

Kenneth KawamotoCommunity ExpertCorrect answer
Community Expert
August 8, 2011

If you just open a file and write it AIR does not prompt you - there's nothing to prompt.

The reason you get the prompt is you are calling browse. There's no need for that, unless you want the user to choose the file name/location.

For example the following will create a text file on desktop without any prompts whatsoever:

var file:File = File.desktopDirectory.resolvePath("test.txt");
var stream:FileStream = new FileStream();
stream.open(file, FileMode.UPDATE);
stream.writeUTF("this is a test");
stream.close();
August 8, 2011

I know this is for air but can you overwrite in flash player?

Community Expert
August 8, 2011

> I know this is for air but can you overwrite in flash player?

File and FileStream classes are for AIR only

kglad
Community Expert
Community Expert
August 7, 2011

that's an operating system alert and you can't thwart that with anything in air.