Downloading files in Flex using the FileReference class

简介:

OK, enough embedding examples, lets take a look at downloading files using Flash Player’s FileReference class (flash.net.FileReference). This example demonstrates a basic usage of the FileReference class within Flex, allowing users to download a file from the server. This example also shows how you can use data tips in the DataGrid control by setting the data grid column’s showDataTips property to true and specifying a value for the dataTipField column.

Full code after the jump.

 

The following example downloads a ZIP file when the user presses the Button control. You can mouse over the items in the DataGrid control’s “Type” column to see additional event information

<? xml version="1.0" encoding="utf-8" ?>
<!--  http://blog.flexexamples.com/2007/07/28/downloading-files-in-flex-using-the-filereference-class/  -->
< 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 of the file to download. */
            private const FILE_URL:String = "http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test/bin/srcview/FileReference_download_test.zip";   

            private var fileRef:FileReference;
            private var urlReq:URLRequest;   

            private function init():void {
                /* Initialize the array collection to an empty collection. */
                arrColl = new ArrayCollection();   

                /* Set up the URL request to download the file specified by the FILE_URL variable. */
                urlReq = new URLRequest(FILE_URL);   

                /* Define file reference object and add a bunch of event listeners. */
                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 {
                /* Create shortcut to the FileReference object. */
                var fr:FileReference = evt.currentTarget as FileReference;   

                /* Add event order and type to the DataGrid control. */
                arrColl.addItem({data:arrColl.length+1, type:evt.type, eventString:evt.toString()});   

                try {
                    /* Update the 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;
                    /* Display the Text control. */
                    txt.visible = true;
                } catch (err:*) {
                    /* uh oh, an error of sorts. */
                }
            }   

            private function downloadSourceCodeZip():void {
                /* Clear existing array collection. */
                arrColl = new ArrayCollection();
                /* Hide the Text control. */
                txt.visible = false;
                /* Begin download. */
                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 >



    本文转自 OldHawk  博客园博客,原文链接:http://www.cnblogs.com/taobataoma/archive/2008/01/13/1036991.html ,如需转载请自行联系原作者


相关文章
|
Kubernetes Java Serverless
进击的 Serverless:Java 应用如何从容地面对突增流量
进击的 Serverless:Java 应用如何从容地面对突增流量
104440 92
|
SQL 物联网 Apache
使用Apache IoTDB进行IoT相关开发的架构设计与功能实现(11)
目前,IoTDB中不存在冲突的权限,因此用户的真正权限是用户自身权限和用户角色权限的结合。也就是说,要确定用户是否可以执行操作,取决于用户自己的权限之一或用户角色的权限是否允许该操作。用户自己的特权和用户角色的特权可能会重叠,但这并不重要。
552 1
|
自然语言处理 JavaScript
vue element plus Config Provider 全局配置
vue element plus Config Provider 全局配置
682 0
|
Android开发 开发者 UED
个人开发 App 成功上架手机应用市场的关键步骤
个人开发 App 成功上架手机应用市场的关键步骤
|
6月前
|
存储 机器学习/深度学习 安全
阿里云服务器租用价格:最新包年、按小时、按月收费标准及活动价格参考
阿里云服务器租用价格参考,轻量应用云服务器2核2G38元1年,云服务器2核2G3M99元1年、2核4G5M199元1年。本文为大家展示阿里云服务器最新包年、按小时、按月收费标准,以及部分云服务器的最新活动价格情况,以供大家了解阿里云服务器的收费模式,不同实例之间的收费差异,从而根据自己的需求和预算等情况选择适合自己的云服务器实例规格和配置。
|
云安全 机器学习/深度学习 人工智能
课时12:阿里云安全产品之态势感知
阿里云态势感知是基于人工智能的安全产品,帮助企业应对高隐蔽性网络攻击。它通过机器学习全面感知网络威胁,覆盖网络层、主机层和应用层,提供实时入侵检测与响应。具备威胁模型、专家定制、超强检索及全网威胁情报等六大核心优势,显著增强企业网络安全防御能力。在G20峰会期间,成功实现平台用户网站安全运营零干扰。
664 0
|
应用服务中间件
Tomcat打不开startup.bat
Tomcat打不开startup.bat
379 2
|
Cloud Native Java 开发者
新一代Java框架Quarkus的性能优化与应用
新一代Java框架Quarkus的性能优化与应用
|
机器学习/深度学习 人工智能 运维
智能化运维:AI在故障预测与自愈系统中的应用
【6月更文挑战第4天】本文探讨了人工智能(AI)技术在运维领域的革新作用,特别是其在故障预测和自愈系统中的应用。通过分析AI技术的基本原理及其在运维中的实际应用案例,文章揭示了AI如何提升系统的稳定性和效率,同时指出了实施过程中的挑战和未来的发展方向。
|
SQL Oracle 关系型数据库
实时计算 Flink版产品使用合集之使用JDBC方式读取Oracle的number类型时,通过什么方式进行映射
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
513 0
实时计算 Flink版产品使用合集之使用JDBC方式读取Oracle的number类型时,通过什么方式进行映射

热门文章

最新文章