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 ,如需转载请自行联系原作者


通过下载和使用CUDA Toolkit Conda包,您接受了条款。 下载和使用CUDA Toolkit Conda包意味着您同意遵守相关的使用条款和条件。这些条款和条件规定了您可以使用该工具包的方式以及您对其使用的责任。 首先,您必须了解并遵守NVIDIA的许可协议以及其他适用的法律法规。这些许可协议和法律规定了您在使用CUDA Toolkit时的权利和义务。 其次,通过下载和使用CUDA Toolkit Conda包,您应当遵守NVIDIA对软件的许可限制和使用限制。这意味着您只能将软件用于合法和授权的用途,不得将其用于非授权的目的或进行未经许可的修改、分发或更改。 此外,您对使用CUDA Toolkit Conda包的风险和责任也应有所了解。NVIDIA不对软件的适用性、性能和完整性提供任何明示或暗示的保证,并且不对因使用该软件而产生的任何直接或间接的损失负责。您应当自行承担使用该软件的风险,并采取适当的措施来保护您的数据和系统安全。 最后,通过下载和使用CUDA Toolkit Conda包,您也同意遵守NVIDIA对软件更新和升级的要求。您应当及时更新和升级软件,以确保您使用的是最新版本,并享受到新功能和改进带来的好处。 总而言之,通过下载和使用CUDA Toolkit Conda包,您接受了相关的条款和条件,并同意按照这些条款和条件使用该软件。这有助于确保您遵守法律法规,保障软件的合法使用,并最大程度地降低因使用软件而引发的风险和责任。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值