Flex中通过给flash.net包自定一个命名空间在MXML中定义FileReference对象的例子

By Minidxer | August 28, 2008

在前面的Flex中利用FileReference类下载文件的例子中,我们了解了FileReference的基本用法。接下来的例子演示了Flex中通过给flash.net包自定一个命名空间,在MXML中定义FileReference对象。

让我们先来看一下Demo可以右键View Source或点击这里察看源代码):


下面是完整代码(或点击这里察看):

Download: main.mxml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <!-- http://blog.flexexamples.com/2008/08/25/creating-a-filereference-object-using-mxml-in-flex/ -->
  3. <mx:Application name="FileReference_download_test_2"
  4.         xmlns:mx="http://www.adobe.com/2006/mxml"
  5.         xmlns:net="flash.net.*"
  6.         layout="vertical"
  7.         verticalAlign="middle"
  8.         backgroundColor="white">
  9.  
  10.     <mx:Script>
  11.         <![CDATA[
  12.             import mx.controls.Alert;
  13.  
  14.             private const DOWNLOAD_URL:String = "http://blog.flexexamples.com/wp-content/uploads/FileReference_download_test_2/bin/srcview/FileReference_download_test_2.zip";
  15.  
  16.             private function btn_click(evt:MouseEvent):void {
  17.                 var urlReq:URLRequest = new URLRequest(DOWNLOAD_URL);
  18.                 fileReference.download(urlReq);
  19.             }
  20.  
  21.             private function onEvent(evt:Event):void {
  22.                 Alert.show(evt.toString(), evt.type);
  23.             }
  24.         ]]>
  25.     </mx:Script>
  26.  
  27.     <net:FileReference id="fileReference"
  28.             complete="onEvent(event);"
  29.             cancel="onEvent(event);"
  30.             httpStatus="onEvent(event);"
  31.             ioError="onEvent(event);" />
  32.  
  33.     <mx:Button id="btn"
  34.             label="Download source"
  35.             click="btn_click(event);" />
  36.  
  37. </mx:Application>
代码:Peter deHaan 翻译/整理/编译:中文Flex例子

Topics: Flex | No Comments » | 249 views Tags: , ,

Search Posts