专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > Flex

Flex中应用FileReference类下载文件

发布时间:2011-06-27 19:46:56 文章来源:www.iduyao.cn 采编人员:星星草
Flex中使用FileReference类下载文件

下面的实例演示了Flex中的 FileReference 类的基本用法,允许用户从服务器上下载一个文件。这个例子也演示了你可以在 DataGrid组件中显示数据提示(data tips) ,只要把 data grid column 的 showDataTips 属性设置为 true ,然后把 column 的 dataTipField 设置一个值就行了。

在下边的演示中,当用户点击按钮的时候会下载一个 zip 文件 ,然后你可以把鼠标移到 DataGrid 组件的 Type 列上,来看额外的 Event 信息。

 

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" verticalAlign="middle" backgroundColor="white" creationComplete="init();">
  
  <mx:Script>
    <![CDATA[
      import mx.controls.Alert;
      import mx.collections.ArrayCollection;
      import flash.net.FileReference;

      [Bindable]
      [Embed('assets/disk.png')]
      private var diskIcon:Class;
      
      [Bindable]
      private var arrColl:ArrayCollection;

      // 要下载文件的URL
      private const FILE_URL:String = "http://www.nshen.net/blog/doc/flex/FileReference_download_test/FileReference_download_test.zip";

      private var fileRef:FileReference;
      private var urlReq:URLRequest;
      
      private function init():void {
        // 初始化一个空ArrayCollection
        arrColl = new ArrayCollection();
        
        // 以FILE_URL指定的地址建立一个URLRequest
        urlReq = new URLRequest(FILE_URL);
        
        // 定义一个FileReference对象,并填加一系列事件监听
        fileRef = new FileReference();
        fileRef.addEventListener(Event.CANCEL, doEvent);
        fileRef.addEventListener(Event.COMPLETE, doEvent);
        fileRef.addEventListener(Event.OPEN, doEvent);
        fileRef.addEventListener(Event.SELECT, doEvent);
        fileRef.addEventListener(HTTPStatusEvent.HTTP_STATUS, doEvent);
        fileRef.addEventListener(IOErrorEvent.IO_ERROR, doEvent);
        fileRef.addEventListener(ProgressEvent.PROGRESS, doEvent);
        fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, doEvent);
      }
      
      private function doEvent(evt:Event):void {
        // 取得当前FileReference的引用
        var fr:FileReference = evt.currentTarget as FileReference;
        
        // 填加事件顺序和类型到DataGrid组件
        arrColl.addItem({data:arrColl.length+1, type:evt.type, eventString:evt.toString()});

        try {
          // 更新 Model.
          fileRefModel.creationDate = fr.creationDate;
          fileRefModel.creator = fr.creator;
          fileRefModel.modificationDate = fr.modificationDate;
          fileRefModel.name = fr.name;
          fileRefModel.size = fr.size;
          fileRefModel.type = fr.type;
          // 显示文本
          txt.visible = true;
        } catch (err:*) {
          // uh oh, an error of sorts.
        }
      }

      private function downloadSourceCodeZip():void {
        // 清空现有的 array collection.
        arrColl = new ArrayCollection();
        // 隐藏文本组件
        txt.visible = false;
        // 开始下载
        fileRef.download(urlReq);
      }
      
      private function showAlert(item:Object):void {
        Alert.show(item.eventString, item.type);
      }
    ]]>
  </mx:Script>
  
  <mx:Model id="fileRefModel">
    <file>
      <creationDate>{""}</creationDate>
      <creator>{""}</creator>
      <modificationDate>{""}</modificationDate>
      <name>{""}</name>
      <size>{""}</size>
      <type>{""}</type>
    </file>
  </mx:Model>
  
  <mx:Button id="downloadBtn" label="Download example source code" icon="{diskIcon}" click="downloadSourceCodeZip()" toolTip="{FILE_URL}" height="40" />

  <mx:DataGrid id="debug" dataProvider="{arrColl}" width="{downloadBtn.width}" rowCount="5" rowHeight="22" itemClick="showAlert(event.currentTarget.selectedItem)">
    <mx:columns>
      <mx:DataGridColumn dataField="data" headerText="#" width="20" />
      <mx:DataGridColumn dataField="type" headerText="Type" showDataTips="true" dataTipField="eventString" />
    </mx:columns>
  </mx:DataGrid>
  
  <mx:Text id="txt" condenseWhite="true" visible="false">
    <mx:text>
    creationDate: {fileRefModel.creationDate}
    creator: {fileRefModel.creator}
    modificationDate: {fileRefModel.modificationDate}
    name: {fileRefModel.name}
    size: {fileRefModel.size}
    type: {fileRefModel.type}
    </mx:text>
  </mx:Text>

</mx:Application>

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: