Flex 读写本地文件(Flash Player 10)

     FileReference 类提供了在用户计算机和服务器之间上载和下载文件的方法。操作系统对话框会提示用户选择要上载的文件或用于下载的位置。每个 FileReference对象都引用用户磁盘上的一个文件并且具有一些属性,这些属性包含有关文件大小、类型、名称、创建日期、修改日期和创建者类型(仅限 Macintosh)的信息。 

     Flash Player10中一个新特性就是更新了ActionScript FileReference API,新的FileReference允许Flash能够直接读写用户系统的数据。在10之前,如果要读写用户的本地文件,Flash只能先把它(文件)返回Server端,然后从Server端加载,之后才可访问,或者下载......

     需要注意的是:save和load API只能是在用户交互才能使用例如 用户单击事件

 

     下面提供两个例子:

     readfile

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  

    <mx:Script>  
        <![CDATA[  
            import flash.net.FileReference;  
            import flash.net.FileFilter;  
  
            import flash.events.IOErrorEvent;  
            import flash.events.Event;  
  
            import flash.utils.ByteArray;  
  
            //FileReference Class well will use to load data  
            private var fr:FileReference;  
  
            //File types which we want the user to open  
            private static const FILE_TYPES:Array = [new FileFilter("text File", "*.txt")];  
  
            //called when the user clicks the load file button  
            private function onLoadFileClick():void  
            {  
                //create the FileReference instance  
                fr = new FileReference();  
  
                //listen for when they select a file  
                fr.addEventListener(Event.SELECT, onFileSelect);  
  
                //listen for when then cancel out of the browse dialog  
                fr.addEventListener(Event.CANCEL,onCancel);  
  
                //open a native browse dialog that filters for text files  
                fr.browse(FILE_TYPES);  
            }  
  
            /************ Browse Event Handlers **************/  
  
            //called when the user selects a file from the browse dialog  
            private function onFileSelect(e:Event):void  
            {  
                //listen for when the file has loaded  
                fr.addEventListener(Event.COMPLETE, onLoadComplete);  
  
                //listen for any errors reading the file  
                fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);  
  
                //load the content of the file  
                fr.load();  
            }  
  
            //called when the user cancels out of the browser dialog  
            private function onCancel(e:Event):void  
            {  
                trace("File Browse Canceled");  
                fr = null;  
            }  
  
            /************ Select Event Handlers **************/  
  
            //called when the file has completed loading  
            private function onLoadComplete(e:Event):void  
            {  
                //get the data from the file as a ByteArray  
                var data:ByteArray = fr.data;  
  
                //read the bytes of the file as a string and put it in the  
                //textarea  
                outputField.text = data.readUTFBytes(data.bytesAvailable);  
  
                //clean up the FileReference instance  
  
                fr = null;  
            }  
  
            //called if an error occurs while loading the file contents  
            private function onLoadError(e:IOErrorEvent):void  
            {  
                trace("Error loading file : " + e.text);  
            }  
  
        ]]>  
    </mx:Script>  
  
    <mx:Button label="Load Text File" right="10" bottom="10" click="onLoadFileClick()"/>  
    <mx:TextArea right="10" left="10" top="10" bottom="40" id="outputField"/>  
  
</mx:Application>  

 

     writefile

<?xml version="1.0" encoding="utf-8"?>  
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  
  
    <mx:Script>  
        <![CDATA[  
  
            import flash.net.FileReference;  
  
            import flash.events.IOErrorEvent;  
            import flash.events.Event;  
  
            private static const DEFAULT_FILE_NAME:String = "example.txt";  
  
            //FileReference Class well will use to save data  
            private var fr:FileReference;  
  
            /********** UI Event Handlers **************/  
  
            //called when the users types in the textarea  
            //note valueCommit should be used, but is broken in the flex build   
            //I am using  
            private function onInputChange():void  
            {  
                //enable button if there is any text to save  
                saveButton.enabled = (inputField.text.length > 0);  
            }  
  
            //called when the user clicks the load file button  
            private function onSaveClick():void  
            {  
                //create the FileReference instance  
                fr = new FileReference();  
  
                //listen for the file has been saved  
                fr.addEventListener(Event.COMPLETE, onFileSave);  
  
                //listen for when then cancel out of the save dialog  
                fr.addEventListener(Event.CANCEL,onCancel);  
  
                //listen for any errors that occur while writing the file  
                fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);  
  
                //open a native save file dialog, using the default file name  
                fr.save(inputField.text, DEFAULT_FILE_NAME);
                
                 
            }  
  
            /***** File Save Event Handlers ******/  
  
            //called once the file has been saved  
            private function onFileSave(e:Event):void  
            {  
                trace("File Saved");  
                fr = null;  
            }  
  
            //called if the user cancels out of the file save dialog  
            private function onCancel(e:Event):void  
            {  
                trace("File save select canceled.");  
                fr = null;  
            }  
  
            //called if an error occurs while saving the file  
            private function onSaveError(e:IOErrorEvent):void  
            {  
                trace("Error Saving File : " + e.text);  
                fr = null;  
            }  
        ]]>  
    </mx:Script>  
  
    <mx:Button label="Save File" right="10" bottom="10" id="saveButton"  
                                            click="onSaveClick()" enabled="false"/>  
    <mx:TextArea right="10" left="10" top="36" bottom="40" id="inputField"  
                                    editable="true" change="onInputChange()"/>  
    <mx:Label text="Enter text to save" left="10" top="10" fontWeight="bold"/>  
  
</mx:Application>  

     除了在上面的例子中所示的事件,还播出了由下列事件的API:

     ProgressEvent.PROGRESS:提供读取或写入该文件的进展

     Event.OPEN:广播时,打开文件进行读取或写入。

 

你可能感兴趣的:(Flex,actionscript)