分享

读写本地文件 in Flash Player 10

 quasiceo 2015-01-27


     在Flash Player 10之前,为了能够读写用户的本地文件,Flash首先不得不先把它反弹回sever端,然后再从server端加载它,之后才可以访问它,这样不仅编程实现起来麻烦,而且还增加了应用的延迟和资源的使用。


     The new functionality is achieved through the addition of two new APIs on the FileReference class:


FileReference.load() : Loads data from a file selected by the user.

FileReference.save() : Saves data to a file location selected by the user.




需要记住以下几点:

  ·The load() and save() APIs can only be called in response to user interaction (such as a button click).

  ·The locations of the loaded and save files are not exposed to ActionScript.

  ·The APIs are asynchronous (异步的)(non-blocking).




Below are two examples that show how to use the APIs. The examples use Flex for the UI, but the ActionScript is the same regardless of whether you are using Flex or not.


Read a file from the users system: 






  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  

  3.   

  4.     <mx:Script>  

  5.         <!--[CDATA[  

  6.             import flash.net.FileReference;  

  7.             import flash.net.FileFilter;  

  8.   

  9.             import flash.events.IOErrorEvent;  

  10.             import flash.events.Event;  

  11.   

  12.             import flash.utils.ByteArray;  

  13.   

  14.             //FileReference Class well will use to load data  

  15.             private var fr:FileReference;  

  16.   

  17.             //File types which we want the user to open  

  18.             private static const FILE_TYPES:Array = [new FileFilter("Text File", "*.txt;*.text")];  

  19.   

  20.             //called when the user clicks the load file button  

  21.             private function onLoadFileClick():void  

  22.             {  

  23.                 //create the FileReference instance  

  24.                 fr = new FileReference();  

  25.   

  26.                 //listen for when they select a file  

  27.                 fr.addEventListener(Event.SELECT, onFileSelect);  

  28.   

  29.                 //listen for when then cancel out of the browse dialog  

  30.                 fr.addEventListener(Event.CANCEL,onCancel);  

  31.   

  32.                 //open a native browse dialog that filters for text files  

  33.                 fr.browse(FILE_TYPES);  

  34.             }  

  35.   

  36.             /************ Browse Event Handlers **************/  

  37.   

  38.             //called when the user selects a file from the browse dialog  

  39.             private function onFileSelect(e:Event):void  

  40.             {  

  41.                 //listen for when the file has loaded  

  42.                 fr.addEventListener(Event.COMPLETE, onLoadComplete);  

  43.   

  44.                 //listen for any errors reading the file  

  45.                 fr.addEventListener(IOErrorEvent.IO_ERROR, onLoadError);  

  46.   

  47.                 //load the content of the file  

  48.                 fr.load();  

  49.             }  

  50.   

  51.             //called when the user cancels out of the browser dialog  

  52.             private function onCancel(e:Event):void  

  53.             {  

  54.                 trace("File Browse Canceled");  

  55.                 fr = null;  

  56.             }  

  57.   

  58.             /************ Select Event Handlers **************/  

  59.   

  60.             //called when the file has completed loading  

  61.             private function onLoadComplete(e:Event):void  

  62.             {  

  63.                 //get the data from the file as a ByteArray  

  64.                 var data:ByteArray = fr.data;  

  65.   

  66.                 //read the bytes of the file as a string and put it in the  

  67.                 //textarea  

  68.                 outputField.text = data.readUTFBytes(data.bytesAvailable);  

  69.   

  70.                 //clean up the FileReference instance  

  71.   

  72.                 fr = null;  

  73.             }  

  74.   

  75.             //called if an error occurs while loading the file contents  

  76.             private function onLoadError(e:IOErrorEvent):void  

  77.             {  

  78.                 trace("Error loading file : " + e.text);  

  79.             }  

  80.   

  81.         ]]-->  

  82.     </mx:Script>  

  83.   

  84.     <mx:Button label="Load Text File" right="10" bottom="10" click="onLoadFileClick()"/>  

  85.     <mx:TextArea right="10" left="10" top="10" bottom="40" id="outputField"/>  

  86.   

  87. </mx:Application>  



 


Write a file to the users system:






  1. <?xml version="1.0" encoding="utf-8"?>  

  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">  

  3.   

  4.     <mx:Script>  

  5.         <!--[CDATA[  

  6.   

  7.             import flash.net.FileReference;  

  8.   

  9.             import flash.events.IOErrorEvent;  

  10.             import flash.events.Event;  

  11.   

  12.             private static const DEFAULT_FILE_NAME:String = "example.txt";  

  13.   

  14.             //FileReference Class well will use to save data  

  15.             private var fr:FileReference;  

  16.   

  17.             /********** UI Event Handlers **************/  

  18.   

  19.             //called when the users types in the textarea  

  20.             //note valueCommit should be used, but is broken in the flex build   

  21.             //I am using  

  22.             private function onInputChange():void  

  23.             {  

  24.                 //enable button if there is any text to save  

  25.                 saveButton.enabled = (inputField.text.length > 0);  

  26.             }  

  27.   

  28.             //called when the user clicks the load file button  

  29.             private function onSaveClick():void  

  30.             {  

  31.                 //create the FileReference instance  

  32.                 fr = new FileReference();  

  33.   

  34.                 //listen for the file has been saved  

  35.                 fr.addEventListener(Event.COMPLETE, onFileSave);  

  36.   

  37.                 //listen for when then cancel out of the save dialog  

  38.                 fr.addEventListener(Event.CANCEL,onCancel);  

  39.   

  40.                 //listen for any errors that occur while writing the file  

  41.                 fr.addEventListener(IOErrorEvent.IO_ERROR, onSaveError);  

  42.   

  43.                 //open a native save file dialog, using the default file name  

  44.                 fr.save(inputField.text, DEFAULT_FILE_NAME);  

  45.             }  

  46.   

  47.             /***** File Save Event Handlers ******/  

  48.   

  49.             //called once the file has been saved  

  50.             private function onFileSave(e:Event):void  

  51.             {  

  52.                 trace("File Saved");  

  53.                 fr = null;  

  54.             }  

  55.   

  56.             //called if the user cancels out of the file save dialog  

  57.             private function onCancel(e:Event):void  

  58.             {  

  59.                 trace("File save select canceled.");  

  60.                 fr = null;  

  61.             }  

  62.   

  63.             //called if an error occurs while saving the file  

  64.             private function onSaveError(e:IOErrorEvent):void  

  65.             {  

  66.                 trace("Error Saving File : " + e.text);  

  67.                 fr = null;  

  68.             }  

  69.         ]]-->  

  70.     </mx:Script>  

  71.   

  72.     <mx:Button label="Save File" right="10" bottom="10" id="saveButton"  

  73.                                             click="onSaveClick()" enabled="false"/>  

  74.     <mx:TextArea right="10" left="10" top="36" bottom="40" id="inputField"  

  75.                                     editable="true" change="onInputChange()"/>  

  76.     <mx:Label text="Enter text to save" left="10" top="10" fontWeight="bold"/>  

  77.   

  78. </mx:Application>  



 


In addition to the events shown in the examples above, the following events are also broadcast by the APIS:


ProgressEvent.PROGRESS : Gives progress on the reading or writing of the file

Event.OPEN : Broadcast when the file is opened for reading or writing.

 While it will also be possible to use these APIs in Adobe AIR, in general, you will want to use the AIR File APIs as they provide more functionality and flexibility.


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多