So, I’m doing my first attempts to write some simple apps in Adobe Flex using Flash Builder 4.6. One of funny things I came across was how to generate open file dialog windows and get name of the file which user picked from dialog windows. I guess still have to get used to specific way Flex applications are developed.

Here is quick code snippet of the tiny application which allows to open file dialog box and pick file. File dialog window will be OS specific.

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx"
applicationComplete="initMain()">

  <fx:Script>
    <![CDATA[
			private var file:File = new File();
						
			// Main init function
			private function initMain():void{
			}
			
			// Open file dialog box and pickup file
			private function pickFile(event:MouseEvent):void{
				// Add event listener for file selection
				file.addEventListener(Event.SELECT, openFile);				
				file.browseForOpen("Open");
			}
			
			// Display information about event and file name in text window
			private function openFile(event:Event):void{
				log("Event type: "+event.type);
				log("File: "+file.nativePath);
			}
			
			private function log(msg:String):void{
				txtLog.appendText(msg+"\n");
			}
    ]]>

  </fx:Script>

  <fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
  </fx:Declarations>

  <s:Button x="10" y="317" label="Choose file" click="pickFile(event)"/>
  <s:TextArea id="txtLog" x="10" y="10" width="721" height="305"/>
</s:WindowedApplication>