首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

Flex4本土文件读写

2013-01-19 
Flex4本地文件读写Read a file from the users system:?xml version1.0 encodingutf-8?mx:Applic

Flex4本地文件读写
Read a file from the users system:

<?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;*.text")];

//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>

Write a file to the users system:

<?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>

这是从另外一个地方的摘录,备用:
原文地址:http://www.mikechambers.com/blog/2008/08/20/reading-and-writing-local-files-in-flash-player-10/
但是采用上述方式选择文件列表时,每次最多只能选择800多个,因为之前做的项目中,需要读取的文件比这个数量要大得多,所以不得不放弃了这种方式.当然也有可能是我操作不当,

热点排行