[ActionScript3.0] 使用FileReferenceList处理多个文件上载
package
{
import flash.display.Sprite;
import flash.events.DataEvent;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.ProgressEvent;
import flash.net.FileFilter;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLVariables;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
/**
* ...
* @author Frost.Yen
*/
public class UploadListAS3 extends Sprite
{
private var _label:TextField;
private var _button:TextField;
private var _fileRefer:FileReferenceList;
private var _count:int = 0;
private var _request:URLRequest = new URLRequest("http://localhost/yanzimen/");
public function UploadListAS3()
{
if(stage) init();
else addEventListener(Event.ADDED_TO_STAGE,init);
}
private function init(e:Event = null):void
{
_label = new TextField();
_label.text = "please select file";
_label.autoSize = TextFieldAutoSize.LEFT;
_label.background = true;
_label.width = 300;
_label.wordWrap = true;
_label.backgroundColor = 0xffffff;
_label.border = true;
_button = new TextField();
_button.text = "click here to select file";
_button.autoSize = TextFieldAutoSize.LEFT;
_button.selectable = false;
_button.background = true;
_button.backgroundColor = 0xE29966;
_button.border = true;
this.addChild(_label);
this.addChild(_button);
_button.x = 310; _fileRefer = new FileReferenceList();
_button.addEventListener(MouseEvent.CLICK,toSelect);
_fileRefer.addEventListener(Event.SELECT,onSelect);
_fileRefer.addEventListener(Event.CANCEL,onCancel);
}
private function toSelect(e:MouseEvent):void
{
_fileRefer.browse(); //browse(typeFilter:Array=null) 使用 typeFilter 参数,可决定对话框显示哪些文件。
_button.removeEventListener(MouseEvent.CLICK,toSelect);
}
private function onProgress(e:ProgressEvent):void
{
_label.text = Number(Math.round(e.bytesLoaded / e.bytesTotal*100))+"%";
//label.text = e.bytesLoaded+"/"+e.bytesTotal;
}
private function onComplete(e:Event):void
{
e.target.removeEventListener(Event.COMPLETE,onComplete);
e.target.removeEventListener(ProgressEvent.PROGRESS,onProgress); if(_count<_fileRefer.fileList.length-1){
_label.text = "load complete "+(_count+1)+" file";
_count++;
}else{
_count=0;
_button.addEventListener(MouseEvent.CLICK,toUpload);
_label.text = "all files load complete!";
_button.text = "to upload";
}
}
private function toUpload(e:MouseEvent):void{
onUpLoad(_fileRefer.fileList[_count]);
}
private function onSelect(e:Event):void
{
_label.text = "";
for(var i:int = 0;i<_fileRefer.fileList.length;i++){
trace("the name of the selected file:",_fileRefer.fileList[i].name);
_label.appendText("file "+(i+1)+":"+_fileRefer.fileList[i].name+"\n");
_fileRefer.fileList[i].addEventListener(Event.COMPLETE,onComplete);
_fileRefer.fileList[i].addEventListener(ProgressEvent.PROGRESS,onProgress);
(_fileRefer.fileList[i] as FileReference).load();
}
}
private function onCancel(e:Event):void
{
trace("cancel to upload");
_button.addEventListener(MouseEvent.CLICK,toSelect);
}
/**
* 调用FileReference的实例方法upload()实现文件上传
* @param file
*/
private function onUpLoad(file:FileReference):void
{
_label.text = "file "+(_count+1)+" size"+Number(file.size/1024/1024).toFixed(2)+"M";
var variables:URLVariables = new URLVariables();
variables.fileName = file.name;
_request.data = variables;
_request.method = URLRequestMethod.POST;
file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
file.addEventListener(Event.COMPLETE, completeHandler);
file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeDataHandler);
try
{
file.upload(_request);
}
catch(error:Error)
{
trace(error.message);
} function progressHandler(e:ProgressEvent):void
{
_label.text = "file "+(_count+1)+" uploading "+Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
}
function completeHandler(e:Event):void
{
var file:FileReference = FileReference(e.target);
file.removeEventListener(ProgressEvent.PROGRESS, progressHandler);
file.removeEventListener(Event.COMPLETE, completeHandler);
_button.removeEventListener(MouseEvent.CLICK,toUpload);
_button.addEventListener(MouseEvent.CLICK,toSelect); if(_count<_fileRefer.fileList.length-1){
_label.text = "upload complete "+(_count+1)+" file";
_count++;
onUpLoad(_fileRefer.fileList[_count]);
}else{
_label.text = "all files upload complete";
_button.text = "click here to select file";
}
}
function completeDataHandler(e:DataEvent):void
{
var file:FileReference = FileReference(e.target);
file.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,completeHandler);
if(_count==_fileRefer.fileList.length-1){
trace("upload_complete_data",e.data);//上传成功后,从后台返回的数据
}
}
}
/**
* FileFilter 类用于表示在调用 FileReference.browse() 方法、FileReferenceList.browse() 方法或调用 File、FileReference 或 FileReferenceList 对象的 browse 方法时显示的文件浏览对话框中显示用户系统上的哪些文件。
* @return
*/
private function getImageTypeFilter():FileFilter {
return new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
} private function getTextTypeFilter():FileFilter {
return new FileFilter("Text Files (*.txt, *.rtf)", "*.txt;*.rtf");
}
}
}
[ActionScript3.0] 使用FileReferenceList处理多个文件上载的更多相关文章
- 【ActionScript】ActionScript3.0对舞台组件的增删改查
以一个样例来说明ActionScript3.0对舞台组件的增删改查 例如以下图: 在Flash执行的时候,通过脚本.斜向下生成text0-text9十个文本节点. 提供两个功能. 1.在右上角,用户输 ...
- 原创教程“ActionScript3.0游戏中的图像编程”開始连载啦!
经过近两年的不懈努力,笔者的原创教程"ActionScript3游戏中的图像编程"最终在今日划上了完美的句号!这其中记录着笔者多年来在游戏制作,尤其是其中图像处理方 ...
- ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0,现把学习结果分享一下,希望对新手有帮助. 目录 ActionScript 3.0简介 Hello ...
- System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件
重新系统后,iis asp.net站点老是出现: System.Web.HttpCompileException (0x80004005): (0): error CS0016: 未能写入输出文件“c ...
- [转]ActionScript 3.0入门:Hello World、文件读写、数据存储(SharedObject)、与JS互调
本文转自:http://www.cnblogs.com/artwl/p/3396330.html 近期项目中可能要用到Flash存取数据,并与JS互调,所以就看了一下ActionScript 3.0, ...
- ActionScript3.0(AS3)中的泛型数组Vector
Adobe官方并没有"泛型数组"的叫法,这是我自己对Vector的叫法(有点标题党),不过Vector在使用上确实跟c#中的泛型数组有些相似之处. 原作者:菩提树下的杨过出处:ht ...
- Yii2.0源码分析之——控制器文件分析(Controller.php)创建动作、执行动作
在Yii中,当请求一个Url的时候,首先在application中获取request信息,然后由request通过urlManager解析出route,再在Module中根据route来创建contr ...
- Cocos2d-x 3.0 打造一个全平台概念文件夹
Cocos2d-x 3.0 打造一个全平台概念文件夹http:// www.eoeandroid.com/thread-328055-1-1.html
- hbase启动的时候报:cat: /home/hadoop/hbase-0.94.6-cdh4.5.0/target/cached_classpath.txt: 没有那个文件或目录
启动hbase的时候: -cdh4.5.0/bin$ hbase shell cat: /home/hadoop/hbase--cdh4.5.0/target/cached_classpath.txt ...
随机推荐
- ConcurrentDictionary内部机制粗解
ConcurrentDictionary是线程安全类,是什么在保证? 内部类 private class Tables { internal readonly Node[] m_buckets; // ...
- Display file information in the document window
[Display file information in the document window] The status bar is located at the bottom of every d ...
- windchill StatementCache: wt.util.Cache%828007782 [size=50, count=4, hits=36, misses=4, aged=0]
StatementCache: wt.util.Cache%828007782 [size=50, count=4, hits=36, misses=4, aged=0] 方法: EXEC sys.s ...
- 06-SSH综合案例:前台首页访问
1.5 编码实现: 1.5.1 首页显示: 复制所有文件到工程下: * css * js * image 复制页面到工程WEB-INF/jsp/ * 将后缀.htm改为jsp 访问一个Actio ...
- 电话号码的字母组合 · Letter Combinations of a Phone Number
[抄题]: Given a digit string excluded 01, return all possible letter combinations that the number coul ...
- Spring Boot 响应jsp
添加依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http:/ ...
- 在MS Test中如何测试private方法
前言: 在博客开始之前,我们先讨论一下是否应该对private方法做测试,通常有两种观点: private方法应该被测试: private方法不应该被测试: 我们以下面的代码为例子来进行说明: pub ...
- Text Relatives
[Text Relatives] With TextKit the resources at your disposal range from framework objects—such as te ...
- Linux上编译hadoop-2.7.1的libhdfs.so和libhdfs.a
hadoop提供了CMake来编译libhdfs,因此在编译之前需要先安装好CMake工具. 然后进入libhdfs的源代码目录,如:/data/hadoop-2.7.1-src/hadoop-hdf ...
- __lll_mutex_lock_wait的错误原因
1. x86_64栈(glib 2.4): free时: (gdb) bt #0 0x00002b9405ea1c38 in __lll_mutex_lock_wait () from /lib64 ...