2016-10-13 00:16:48 点击量:10 标签: 收藏本文
AS3中FileRefence类提供了上传和下载文件的方法,它可以打开一个对话框供用户用于上传和下载文件的位置,做了一个简单的文件下载程序练习下FileRefence类中方法和事件的使用。
先在舞台上添加一个输入文本框,一个progressBar组件和两个按钮,命名为"fileURL","downloadProgress","download","cancel";
- var fr:FileReference = new FileReference();
- fileURL.text = "player_img_path/20161202/bacz5jlekmt.jpg"
- fr.addEventListener(Event.OPEN,openHandler);
- fr.addEventListener(ProgressEvent.PROGRESS,progressHandler);
- fr.addEventListener(Event.COMPLETE,completeHandler);
- fr.addEventListener(IOErrorEvent.IO_ERROR,ioerrorHd);
- download.addEventListener(MouseEvent.CLICK,startDownload);
- cancel.addEventListener(MouseEvent.CLICK,cancelDownload);
- function cancelDownload(event:MouseEvent):void {
- fr.cancel();
- download.enabled = false;
- }
- function startDownload(event:MouseEvent):void {
- var request:URLRequest = new URLRequest();
- request.url = fileURL.text;
- fr.download(request);
- }
- function openHandler(event:Event):void {
- download.enabled = true;
- trace("open");
- }
- //CuPlayer.com提示:下载处理函数,用进度条显示下载速度
- function progressHandler(event:ProgressEvent):void {
- downloadProgress.setProgress(event.bytesLoaded,event.bytesTotal);
- trace("prog");
- }
- function completeHandler(event:Event):void {
- downloadProgress.setProgress(0,100);
- download.enabled = false;
- trace("ok");
- }
- function ioerrorHd(event:IOErrorEvent) {
- trace("CuPlayer.com提示你:不能下载该文件");
- }
点击下载按钮后程序读取文本框中的地址,创建URLRequest对象,调用download()方法实现下载。
我们是一家提供综合软件外包与弱电工程服务的技术公司。
业务涵盖定制软件、管理系统、网站、小程序、APP、系统集成、网络布线、监控安防、门禁、机房建设及长期技术维护。
公司地址:湖北省宜昌市伍家岗区东站二路6号三峡(宜昌)大数据产业园海洋馆二楼A203室