code How to create “Browse for folder” dialog in Adobe FLEX?

calculator in c# code project (4)

This is a quick function set to create a nice folder browser in Flex:

private var file:File = new File();

private function pickFile(event:MouseEvent):void {
    file.addEventListener(Event.SELECT, openFile);              
    file.browseForDirectory("Select folder...");

private function openFile(event:Event):void{
    folderPath.text = file.nativePath;

The first function deals with the folder browser, the second one populates a text input with the full folder path.


On the stage, create a simple mx:button and add a call to the pickFile() function for the click event:

<mx:Button click="{pickFile(event);}" />

Then, put also on the stage an mx:TextInput component, to show the folder path after selection:

<mx:TextInput id="folderPath" editable="false" />

This way you have a button to click in order to show the system folder browser, and a text input to show the full folder path after selection.

To improve the button look, you can embed a nice folder icon :-)

Just my 2c. :-)

Know someone, how to create "Browse for folder" dialog in Adobe FLEX? And it is possible?


Answer #1

File class AS3 / AIR: let user choose only save location, not file name or extension

For a reference on how to open a browse dialog to choose a folder see the example here:

How to create "Browse for folder" dialog in Adobe FLEX?

once you have your directory you can piece that together with code here to save a file using the FileStream object:

copied here since it's an external link

    private function saveFile():void{
        var myPattern:RegExp = / /g;
        var newFileName:String = fileName.text.replace('.txt','');
        if(newFileName.length > 1){
            var file:File = File.desktopDirectory.resolvePath("Files/" + newFileName.replace(myPattern,'_') + ".txt");
            var stream:FileStream = new FileStream()
  , FileMode.WRITE);
            var str:String = contents.text;
            str = str.replace(/\r/g, File.lineEnding);
   = File.desktopDirectory.resolvePath("Files/");
            fileName.text = "";
            contents.text = "";
          } else {
    "File name is required", "Error Saving File");

Answer #2

in Web, for multiple file upload, (for single file upload, use FileRefernce)

private var _refAddFiles:FileReferenceList;
private function browse():void
    _refAddFiles = new FileReferenceList();
    var fileFilter:FileFilter=new FileFilter("*.jpg","*.jpg;*.jpeg;");
    _refAddFiles.addEventListener(Event.SELECT, onSelectFile);

<mx:Button click="browse"/>

This will work, and what you want to do after selection,

private function onSelectFile(event:Event):void
    _arrUploadFiles = [ ];
    if (_refAddFiles.fileList.length >= 1)
        for (var k:Number = 0; k < _refAddFiles.fileList.length; k++)
            _arrUploadFiles.push({ name: _refAddFiles.fileList[k].name,
                                    file: _refAddFiles.fileList[k]});


Answer #3

If it's an Air app you can do :

var f : File = new File;
f.addEventListener(Event.SELECT, onFolderSelected);
f.browseForDirectory("Choose a directory");

If it's a pure As3 app, you cannot Browse for folder, you can just browse for file via FileReference class.