Adobe AIR tips serie: browseForSave set default file extension and validate/modify.

November 17th, 2008 | By Cedric Madelaine

postImage 01short Adobe AIR tips serie: browseForSave set default file extension and validate/modify. image
spacer Adobe AIR tips serie: browseForSave set default file extension and validate/modify. image

In this serie I propose some quick tips for Adobe AIR products developers, which came out during my regular practice of this technology. Today topic is:

Conforming extension for a browseForSave.

This short tip allows you to ensure that the file which will be saved following a browseForSave match legal extension you have defined beforehand and if not it will apply a default extension when using the browse for save functionality. Currently File.extension is a read only property and you cannot change it directly, so I decided to manipulate the path in order to allow it. I think a setExtension and allowedExtensions property might be useful in such a situation.

import flash.filesystem.File;
import flash.filesystem.FileStream;
import flash.events.Event;

private var docsDir:File;
private const MY_DEFAULT_EXTENSION:String = "abc";
private const VALID_EXTENSIONS_LIST:Array = ["abc"];

public function mySave():void
{
	docsDir = File.desktopDirectory;
	docsDir.browseForSave("Save As");
	docsDir.addEventListener(Event.SELECT, mySaveHandler);
}
private function mySaveHandler(event:Event):void
{
	docsDir.removeEventListener(Event.SELECT, mySaveHandler);
	//Split the returned File native path to retrieve file name
	var tmpArr:Array = File(event.target).nativePath.split(File.separator);
	var fileName:String = tmpArr.pop();//remove last array item and return its content
	//Check if the extension given by user is valid, if not the default on is put.
	//(for example if user put himself/herself an invalid file extension it is removed in favour of the default one)
	var conformedFileDef:String = conformExtension(fileName);//comment: updated 17.11.2008 removed the typo
	tmpArr.push(conformedFileDef);
	//Create a new file object giving as input our new path with conformed file extension
	var conformedFile:File = new File("file:///" + tmpArr.join(File.separator));
	//Make save
	var stream:FileStream = new FileStream();
	stream.open(conformedFile, FileMode.WRITE);
	stream.writeUTFBytes("demo demo demo demo demo demo demo demo demo");
	stream.close();
}
private function conformExtension(fileDef:String):String
{
	var fileExtension:String = fileDef.split(".")[1];
	for each(var it:String in VALID_EXTENSIONS_LIST){
		if( fileExtension == it)
			return fileDef;

	}
	return fileDef.split(".")[0] + "." + MY_DEFAULT_EXTENSION;
}

Feel free to add any comment!

spacer Adobe AIR tips serie: browseForSave set default file extension and validate/modify. image

Related posts:

  1. Live from Max Europe 2008 Serie: Adobe HAIR Head Application (d1)
  2. 3d realtime apis presentation at Adobe User Group
  3. Export character and paragraph styles for Flex/Flash as css from Indesign
  4. Flex 3 and AIR 1.0 Final Launch
  5. Live from Max Europe 2008 Serie: Preparing to leave (d-2)
  6. Adobe AIR Final Logo revealed
  7. Papervision3D Demo: move Pivot-Axis only

3 Responses to “Adobe AIR tips serie: browseForSave set default file extension and validate/modify.”

  1. 1
    byvisual says:

    Very useful piece of code…

    on thing though.. you have a typo within the code…

    line 24 :

    var conformedFileDef:String = conformExtension(tmpArr.pop());

    shouldn’t it be:

    var conformedFileDef:String = conformExtension(fileName);

    great job.. though..

    thanks for posting this…

    :D

  2. 2
    bLb says:

    I think there is a “pop’ too much in the tmpArr that makes your code not working as you give it:

    var fileName:String = tmpArr.pop();//remove last array item and return its content
    //Check if the extension given by user is valid, if not the default on is put.
    //(for example if user put himself/herself an invalid file extension it is removed in favour of the default one)
    var conformedFileDef:String = conformExtension(tmpArr.pop());

    It works better like this :
    var fileName:String = tmpArr.pop();//remove last array item and return its content
    //Check if the extension given by user is valid, if not the default on is put.
    //(for example if user put himself/herself an invalid file extension it is removed in favour of the default one)
    var conformedFileDef:String = conformExtension(fileName);

    Thanks anyway, your code helps me a lot.

  3. 3
    maddec says:

    Thank you byvisual and bLb for noticing the typo.

    I have corrected it at line 24.

    Happy that you found this piece of code helpful.

Leave a reply