Flash 文件上传组件

朋友有个要求是要在页面上上传文件的时候只能选择指定类型(后缀名)的文件,显然是无能为力的(至少我没有找到办法,想来javascript/DOM也没有这种能力),所以考虑用Flash做一个小组件。
调用方式如下:

id="upload_swf" width="650" height="220"
codebase="http://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab">




//此处向Flash传入自定义变量,注:sessionId是必须的,貌似是Flash的一个Bug

width="650" height="220" name="upload_swf" align="middle"
play="true"
loop="false"
quality="high"
allowScriptAccess="sameDomain"
type="application/x-shockwave-flash"
pluginspage="http://www.adobe.com/go/getflashplayer"
//此处向Flash传入自定义变量,注:sessionId是必须的,貌似是Flash的一个Bug
FlashVars='sessionId=<%=session==null?"":session.getId()%>&uploadPath=datarecover.do&fileFilter=[{"name":"Excel", "postFix": "*.xls"},{"name":"CSV File", "postFix": "*.csv"}]'>



源代码如下:




import mx.rpc.events.ResultEvent;
import mx.collections.ArrayCollection;
import mx.controls.Alert;
import mx.utils.ObjectUtil;
import flash.events.*;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.FileFilter;
import com.adobe.serialization.json.JSON;

private var fileRef:FileReference;
private var sessionId:String;
private var fileFilter:Array;
private var uploadPath:String;


private function initApp():void {
sessionId = Application.application.parameters.sessionId;
var ff:String = String(Application.application.parameters.fileFilter);
fileFilter = JSON.decode(ff) as Array;
uploadPath = Application.application.parameters.uploadPath;
fileRef = new FileReference();
fileRef.addEventListener(Event.CANCEL, traceEvent);
fileRef.addEventListener(Event.COMPLETE, completeEvent);
fileRef.addEventListener(Event.SELECT, selectEvent);
fileRef.addEventListener(IOErrorEvent.IO_ERROR, traceEvent);
fileRef.addEventListener(Event.OPEN, traceEvent);
fileRef.addEventListener(ProgressEvent.PROGRESS, progressEvent);
fileRef.addEventListener(SecurityErrorEvent.SECURITY_ERROR, traceEvent);
}

private function traceEvent(event:Event):void {
//var tmp:String = "";
//ta.text += tmp + event.type + " event:" + mx.utils.ObjectUtil.toString(event) + "\n" ;
//ta.verticalScrollPosition += 20;
}

private function ioErrorEvent(event:IOErrorEvent):void{
Alert.show("IOError:"+event.type+":" + event.text);
traceEvent(event);
}

private function selectEvent(event:Event):void{
btn_upload.enabled = true;
traceEvent(event);
filename.text = fileRef.name;
progressBar.setProgress(0, 100);
progressBar.label = "Loading 0%";
}

private function progressEvent(event:ProgressEvent):void {
progressBar.setProgress(event.bytesLoaded, event.bytesTotal);
traceEvent(event);
}

private function completeEvent(event:Event):void {
progressBar.label = "Complete.";
filename.text += " uploaded";
traceEvent(event);
btn_upload.enabled = false;
btn_cancel.enabled = false;
}

private function uploadFile():void {
var endpoint:String = uploadPath;
endpoint += ";jsessionid="+sessionId;
endpoint +="?fileType="+fileRef.type;
var req:URLRequest = new URLRequest(endpoint);
req.method = URLRequestMethod.POST;
fileRef.upload(req, "filedata", false);
progressBar.label = "Uploading...";
btn_cancel.enabled = true;
}

private function browseFile():void {
var filters:Array = [];
for(var i:int = 0; i < fileFilter.length; i++) {
var filter:Object = fileFilter[i];
filters.push(new FileFilter(filter.name, filter.postFix));
}
fileRef.browse(filters);
}
]]>





















你可能感兴趣的:(Flex/Flash)