July 08, 2009

Working with local system files in Flex3.2 SDK and FlashPlayer10

Hi,
In below example for accessing the local system xml files and read the xml content and set the input to the List control. The seelcted xml content as shown in Textarea control also. Whenever click the image from the List control its relevant image is shown. It must need the Flex sdk3.2 and Flash player10 then only access the function of the Flash player10 as load() and save() otherwise in the below example cannot be work.

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
<mx:Script>
<![CDATA[
import mx.managers.PopUpManager;
import flash.net.FileReference;
import flash.utils.ByteArray;

private var _fileBrowse:FileReference;
private var _imgStr:String;
private var _listProvider:Array;

private function onClick():void
{
_fileBrowse = new FileReference();
_fileBrowse.addEventListener(Event.CANCEL,onCancel);
_fileBrowse.addEventListener(Event.SELECT,onFileSelect);
_fileBrowse.browse();
}

private function onCancel(evt:Event):void
{
_fileBrowse = null;
}

private function onFileSelect(evt:Event):void
{
_fileBrowse.addEventListener(Event.COMPLETE,onComplete);
_fileBrowse.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
_fileBrowse.load();
}

private function onComplete(evt:Event):void
{
var _data:ByteArray = _fileBrowse.data;
txt_area.text = _data.readUTFBytes(_data.bytesAvailable);
_imgStr = txt_area.text;
_fileBrowse = null;
loadXML();
}

private function ioErrorHandler(evt:IOErrorEvent):void
{
trace("Io Error");
}

private function loadXML():void
{
var _xml:XML;
_listProvider = new Array();

if(_imgStr != "")
{
_xml = new XML(_imgStr);
var _xmlList:XMLList = XMLList(_xml.image);
for(var i:int=0;i<_xmlList.length();i++)
{
_listProvider.push({label:_xmlList[i].@name,path:_xmlList[i].@path});
}
lst.dataProvider = _listProvider;
}
}

private function onItemClick():void
{
img.source = lst.selectedItem.path;
}

]]>
</mx:Script>
<mx:HBox paddingLeft="20" paddingTop="20">
<mx:VBox>
<mx:TextArea id="txt_area" width="400" height="200"/>
<mx:List id="lst" width="400" height="120" itemClick="onItemClick()"/>
<mx:Button label="open" click="onClick()"/>
</mx:VBox>
<mx:HBox>
<mx:Image id="img" width="400" height="400"/>
</mx:HBox>
</mx:HBox>
</mx:Application>

The example xml as follows

<?xml version="1.0" encoding="UTF-8"?>
<images>
<image name="image1" path="http://www.digitalphotoartistry.com/rose1.jpg"/>
<image name="image2" path="http://blogs.warwick.ac.uk/images/spjyoung/2006/02/20/rose.jpg"/>
<image name="image3" path="http://www.mccullagh.org/db9/1ds2-2/rose-side-view.jpg"/>
</images>

No comments:

Post a Comment