20081218

ActionScript 3 上傳檔案

/*
原本是用AS2寫上傳
不過AS2上傳後,無法抓到PHP的回傳值
就改用AS3寫

先在第一個影格建一個按鈕,命名為btn

bg.9sweb.com / by egg
*/


var fileRef = new FileReference();
var uploadURL = new URLRequest();

uploadURL.url = 'upload_data.php';
btn .addEventListener(MouseEvent .MOUSE_DOWN , select_file);
fileRef.addEventListener(Event .SELECT , upload_select);
fileRef.addEventListener(Event .CANCEL , upload_cancel);
fileRef.addEventListener(ProgressEvent .PROGRESS , upload_progress);
fileRef.addEventListener(Event .COMPLETE , upload_complete);
fileRef.addEventListener(Event .OPEN , upload_open);
fileRef.addEventListener(DataEvent .UPLOAD_COMPLETE_DATA, upload_return);

function select_file(event:MouseEvent)
{
fileRef.browse();
}

function upload_select(event:Event)
{
trace(fileRef.name);
trace(fileRef.size);
trace(fileRef.type);
fileRef.upload(uploadURL);
}

function upload_cancel(event:Event)
{
trace("upload_cancel: " + event);
}

function upload_progress(event:ProgressEvent)
{
trace("upload_progress name=" + fileRef.name + " bytesLoaded=" + event.bytesLoaded + " bytesTotal=" + event.bytesTotal);
}


function upload_complete(event:Event)
{
trace("upload_complete: " + event);
}

function upload_open(event:Event)
{
trace("upload_open: " + event);
}

function upload_return(event:DataEvent)
{
trace("upload_return: " + event);
trace(event.data);
}

沒有留言: