宜昌本地软件开发与弱电工程服务商
项目咨询 · 快速响应
您当前的位置: > 博客2 > AS2与AS3技术

[AS3]AS3中FileRefence类上传和下载文件的源代码示例

2016-10-13 00:16:48 点击量:10 标签: 收藏本文

AS3中FileRefence类提供了上传和下载文件的方法,它可以打开一个对话框供用户用于上传和下载文件的位置,做了一个简单的文件下载程序练习下FileRefence类中方法和事件的使用。

先在舞台上添加一个输入文本框,一个progressBar组件和两个按钮,命名为"fileURL","downloadProgress","download","cancel";

  1. var fr:FileReference = new FileReference(); 
  2. fileURL.text = "player_img_path/20161202/bacz5jlekmt.jpg" 
  3. fr.addEventListener(Event.OPEN,openHandler); 
  4. fr.addEventListener(ProgressEvent.PROGRESS,progressHandler); 
  5. fr.addEventListener(Event.COMPLETE,completeHandler); 
  6. fr.addEventListener(IOErrorEvent.IO_ERROR,ioerrorHd); 
  7. download.addEventListener(MouseEvent.CLICK,startDownload); 
  8. cancel.addEventListener(MouseEvent.CLICK,cancelDownload); 
  9.  
  10. function cancelDownload(event:MouseEvent):void { 
  11.  fr.cancel(); 
  12.  download.enabled = false
  13.  
  14. function startDownload(event:MouseEvent):void { 
  15.  var request:URLRequest = new URLRequest(); 
  16.  request.url = fileURL.text; 
  17.  fr.download(request); 
  18.  
  19. function openHandler(event:Event):void { 
  20.  download.enabled = true
  21.  trace("open"); 
  22. //CuPlayer.com提示:下载处理函数,用进度条显示下载速度 
  23. function progressHandler(event:ProgressEvent):void { 
  24.  downloadProgress.setProgress(event.bytesLoaded,event.bytesTotal); 
  25.  trace("prog"); 
  26.  
  27. function completeHandler(event:Event):void { 
  28.  downloadProgress.setProgress(0,100); 
  29.  download.enabled = false
  30.  trace("ok"); 
  31.  
  32. function ioerrorHd(event:IOErrorEvent) { 
  33.  trace("CuPlayer.com提示你:不能下载该文件"); 

点击下载按钮后程序读取文本框中的地址,创建URLRequest对象,调用download()方法实现下载。

ABOUT US
宜昌农人网络科技有限公司

我们是一家提供综合软件外包与弱电工程服务的技术公司。

业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。

公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室