<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" width="120" height="20" initialize="init()">
<mx:Button x="0" y="0" width="120" height="20" id="_but" click="show()" />
<mx:Script>
<![CDATA[
import flash.net.*;
import flash.external.ExternalInterface;
//定义http请求对象
private var _https:URLRequest;
//定义上传的文件对象
private var _file:Object;
//保存传入的配置
private var _params:Object;
//过滤后缀
private var _types:Array;
//上传加载方法
public function init():void{
//获取传入的参数
_params = this.parameters;
//加载容器宽度
if(_params.width){
_but.width = _params.width;
}
if(_params.text){
_but.label = _params.text
}
//加载容器高度
if(_params.height){
_but.height = _params.height;
}
if(_params.selectFlag){
//创建上传文件对象
_file = new FileReferenceList();
}else{
//创建上传文件对象
_file = new FileReference();
}
//监听文件选择事件
_file.addEventListener(Event.SELECT,_selectHeader);
//如果外部处理方法有定义,则监听
if(_params.complete){
//监听上传完成事件
_file.addEventListener(Event.COMPLETE,_completeHeader);
}
//如果外部处理异常方法有定义,则监听
if(_params.error){
//监听http状态
_file.addEventListener(HTTPStatusEvent.HTTP_STATUS,_httpStatusHeader);
//监听io流状态
_file.addEventListener(IOErrorEvent.IO_ERROR,_ioErrorHeader);
}
//滚动条监听
if(_params.progress){
_file.addEventListener(ProgressEvent.PROGRESS,_progressHeader);
}
_types = new Array();
//文件类型
if(_params.filterMsg && _params.filter){
_types.push(new FileFilter(_params.filterMsg,_params.filter));
}else{
//默认限制上传类型
_types.push(new FileFilter("All Files(*.*)","*.*"));
_types.push(new FileFilter("Images (*.jpg,*.png,*.jpeg,*.gif)","*.jpg;*.png;*.jpeg;*.gif"));
_types.push(new FileFilter("Text Files (*.txt, *.rtf)","*.txt;*.rtf"));
}
//创建请求对象
_https= new URLRequest();
//加载请求地址
if(_params.url){
_https.url = _params.url;
}else if(_params.error){
//如果没有传入必传配置:url请求地址,则回调错误
ExternalInterface.call(_params.error,{
msg:"Param Error:url is null!"
});
return;
}
_https.contentType = "multipart/form-data";
_https.method = "POST";
var urldata:URLVariables = new URLVariables();
if(_params.uid){
urldata.userid=_params.uid;
}
_https.data=urldata;
}
//加载上传选择窗口
public function show():void{
_file.browse(_types);
}
//处理监听文件选择的方法
private function _selectHeader(e:Event):void{
if(_params.selectFlag){
}
_file.upload(_https,"file");
}
//处理文件上传完成方法
private function _completeHeader(e:Event):void{
ExternalInterface.call(_params.complete,{
filename:_file.name,
filesize:_file.size
});
}
//处理http请求状态方法
private function _httpStatusHeader(e:HTTPStatusEvent):void{
ExternalInterface.call(_params.error,{
msg:"Net error:"+e.status
});
}
//处理io流异常方法
private function _ioErrorHeader(e:IOErrorEvent):void{
ExternalInterface.call(_params.error,{
msg:"IO error:"+e.text
});
}
//滚动条回调
private function _progressHeader(e:ProgressEvent):void{
ExternalInterface.call(_params.progress,{
"totalsize":e.bytesTotal,
"currsize":e.bytesLoaded
});
}
]]>
</mx:Script>
</mx:Application>
flex 上传
最新推荐文章于 2025-05-22 10:42:42 发布