Set File Extension with Adobe Air?

Is there way to set the extension of a file using Adobe Air’s flash.filesystem?

Using the browseForOpen function you can set the FileFilter, limiting the files types available. However when using browseForSave you can not set a fileFilter or fileExtension. The file extension has to be entered manually in the SaveFileDialog. The File.extension property associated with the selected file is read-only. Does anyone know of a work around or anything that I could do to not enter the file Extension manually.

I found this work around on Adobe’s site:

  1. browseForSave - put hint in title bar as to file type
  2. on Select: check the extension. If exists and is the correct type, go ahead and save. If there is no extension, or the extension is wrong, then append the correct extension on the end.

In my case, I am saving HTML files out. Here is my onSelect function:

Attach Code

private function beginSave():void {
file = File.desktopDirectory;
file.addEventListener(Event.SELECT,doSaveReport);
file.browseForSave(“Save as an HTML file || Don’t forget to put .html at the end”);
}

		private function doSaveReport(evt:Event):void {
			file = File(evt.target);
			
			if(!file.extension || file.extension != "html"){
				trace("no extension");
				file.nativePath += ".html";
			}
			
			stream = new FileStream();
			stream.open(file,FileMode.WRITE);
			stream.writeUTFBytes(setupPrintCSS());
			stream.close();				
		}