`
moguicy
  • 浏览: 8535 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
最近访客 更多访客>>
社区版块
存档分类
最新评论

Flex4本地文件读写

    博客分类:
  • Flex
阅读更多
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多个,因为之前做的项目中,需要读取的文件比这个数量要大得多,所以不得不放弃了这种方式.当然也有可能是我操作不当,
分享到:
评论

相关推荐

    flex4.6 air 读写文本文件

    Adobe AIR则允许开发者创建可以在桌面环境下运行的跨平台应用程序,它提供了访问本地文件系统的能力。 要读取文本文件,我们首先需要在Flex项目中引入`File`和`FileStream`类。这两个类分别属于`flash.filesystem`...

    Flex文件传输方式之Flie

    在这个“Flex文件传输方式之File”的示例中,我们将探讨Flex4如何实现文件上传和下载功能,这对于任何需要用户交互处理本地文件的应用程序都是至关重要的。 在Flex中,`File`类是处理用户选择的本地文件的核心组件...

    flex文件系统应用

    在Flex中,文件系统的操作是通过File和FileStream类来实现的,这些类提供了对本地文件系统的访问和管理能力。 首先,我们要了解Flex中的File类。File类是Flex与本地文件系统交互的基础,它可以代表计算机上的任何...

    flex的as3xls读写excel

    根据给定的信息,本文将详细解释Flex框架中利用as3xls库进行Excel文件读写的实现原理及具体步骤。 ### 一、as3xls简介 as3xls 是一个用于Adobe Flex和ActionScript 3项目的库,它允许开发人员轻松地创建、读取和...

    flex 文件上传 预览下载 及中文名乱码解决问题

    在Flex中,我们可以使用`URLLoader`类来发送数据,`FileReference`类则用于选择和上传本地文件。用户通过点击按钮触发`browse()`方法选择文件,然后调用`upload()`方法将选中的文件上传到服务器。注意,为了防止中文...

    Flex 输出文件到本地的两种方法

    上述代码中,创建了一个FileReference实例,并通过`save`方法直接保存字符串内容到本地文件"1.csv"。这里不需要用户交互选择保存路径,`save`方法会自动弹出一个保存对话框供用户选择。 ### File类与FileReference...

    flex 读XML 写XML 并保存为文件

    1. 使用FileReference类:Flex提供了`flash.net.FileReference`类,可以用来保存数据到本地。首先,创建FileReference对象,然后调用`save()`方法: ```actionscript var fileRef:FileReference = new FileReference...

    flex air 调用C代码

    4. **编写C代码**:你需要编写一个C程序,这个程序需要接受Flex传递的参数,并将结果返回给Flex。C程序可以通过标准输入/输出(stdin/stdout)或者自定义的通信协议来与Flex交互。 5. **打包C代码**:C程序需要编译...

    Flex学习—关于Shareobject对象(本地共享对象)

    本文将深入探讨Flex中的一个重要组件——ShareObject,也称为本地共享对象,它允许应用程序在用户计算机上存储数据,实现数据的持久化。 ShareObject是Flex提供的一种轻量级的数据存储解决方案,它类似于Web浏览器...

    flex自己开发的demo

    Flex提供了`FileReference`类,用于处理用户的本地文件选择和上传操作。用户选择文件后,可以通过`upload()`方法发送到服务器,同时可以设置上传进度的监听器,以实时反馈上传状态。需要注意的是,文件上传需要处理...

    flex 操作XML

    - Flex使用`flash.filesystem.File`类来访问本地文件系统。首先,创建一个`File`对象,指定XML文件的路径,然后使用`FileStream`打开并读取文件内容。 - 示例代码: ```actionscript var file:File = new File(...

    flex前台直接导出excel

    其中一种常见方法是使用AS3XLS或FlexCel这两款库,它们提供了读写Excel文件的功能。 AS3XLS是一个用纯ActionScript编写的库,它可以创建和修改Excel的XLS文件。使用AS3XLS,开发者可以直接在Flex应用中生成Excel...

    flex zip压缩算法

    6. **文件操作**:在AS3.0中,`flash.filesystem`和`flash.net`类库提供了读写本地文件和网络文件的能力。开发者可以使用`FileReference`类来上传和下载文件,使用`FileStream`类来读取和写入二进制数据。 7. **...

    Flex3ReadWriteExcel(flex3导入导出excel表格)

    本知识点主要聚焦于Flex3如何实现数据的读写操作,特别是与Microsoft Excel文件的交互,即“Flex3ReadWriteExcel(flex3导入导出excel表格)”。这在实际业务场景中非常常见,例如数据分析、报表生成以及数据交换等。 ...

    Flex 的 Adobe AIR快速入门

    - **文件读写**: 使用File类的方法读取和保存文本文件到磁盘。 4. **压缩文件和数据** - **数据压缩**: 使用Zip类或者第三方库来压缩文件或数据,这对于减少存储空间或网络传输量非常有用。 5. **在Adobe AIR...

    FLEX问题总汇 (总结篇)

    标准的FLEX应用无法直接访问本地系统资源如C盘或D盘的文件,但如果你使用的是Adobe AIR环境,那么可以访问这些资源。 3. **文件操作**: FLEX本身不支持文件的读写操作。若需进行文件操作,通常需要借助Java或...

    flex做的图片浏览

    在Flex中,File类用于表示本地文件系统中的文件或目录,而FileReference类则提供了与用户选择的文件交互的能力,例如读取、写入和上传文件。在这个图片浏览应用中,开发者可能首先会创建一个File对象来代表用户指定...

    flex datagrid to excel

    综上所述,实现“flex datagrid to excel”功能,你需要理解Flex的DataGrid组件、数据处理、文件操作以及ActionScript编程,将DataGrid中的数据转换为Excel可读格式,并通过用户交互将数据导出到本地文件系统。

    Adoe FlexAir教程

    5. **本地存储和访问**:AIR允许应用程序访问本地文件系统,因此你可以了解如何读写文件,存储和检索用户数据。 6. **网络通信**:学习如何使用Flex进行HTTP请求,与Web服务进行交互,或者使用Socket连接进行实时...

Global site tag (gtag.js) - Google Analytics