12月 05, 2007

用Flex3+php上傳檔案

以下是flex3+php的上傳檔案範例,以Air的型式發佈也很方便

下載原始檔

FLEX










import mx.controls.ProgressBar;
import flash.events.MouseEvent;
import mx.controls.Alert;
import mx.collections.ArrayCollection;
import mx.rpc.events.ResultEvent;
import mx.managers.CursorManager;
import mx.core.Application;

private var urlRequest:URLRequest;
private var fileReferenceList:FileReferenceList;
private var pb:ProgressBar;
private var serverSideScript:String = "http://localhost/flex/place/v2/upload.php";
private function inite():void{
//檔案上傳宣告
urlRequest = new URLRequest(serverSideScript);
fileReferenceList = new FileReferenceList();
fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler);
}
//檔案上傳
private function uploadFile():void {
fileReferenceList.browse();
uploadProgress.setProgress(0,0);
}
private function fileSelectedHandler(event:Event):void {
var fileReference:FileReference;
var fileReferenceList:FileReferenceList = FileReferenceList(event.target);
var fileList:Array = fileReferenceList.fileList;
fileReference = FileReference(fileList[0]);
//監聽上傳進度丟出的事件
fileReference.addEventListener(ProgressEvent.PROGRESS,progressHandler);
//監聽是否完成
fileReference.addEventListener(Event.COMPLETE, uploadCompleteHandler);
fileReference.upload(urlRequest,"Filedata");
statusText.text = "上傳中...";
CursorManager.setBusyCursor();
}
private function progressHandler(event:ProgressEvent):void{
uploadProgress.setProgress(event.bytesLoaded, event.bytesTotal);
}

private function uploadCompleteHandler(event:Event):void {
statusText.text = "上傳成功的文件: " +event.target.name ;
CursorManager.removeBusyCursor();
Alert.show(event.target.name+"上傳成功")

}
//檔案上傳結束

]]>








php



$uploaddir = 'uploads/';
$uploadfile = $uploaddir . basename($_FILES['Filedata']['name']);
$temploadfile = $_FILES['Filedata']['tmp_name'];
move_uploaded_file($temploadfile , $uploadfile);
?>



沒有留言: