網上找了一個用fileRefrenceList作為檔案的選擇,貌似用這種方法不能限定上傳檔案的類型,不過經過測試,確實能啟動並執行。
為方便起見,把它封裝成了一個類,並加上了注釋,便於大家和自己以後使用。
UploadImage.as:
package exchage
{
import flash.events.Event;
import flash.net.FileReference;
import flash.net.FileReferenceList;
import flash.net.URLRequest;
import mx.controls.Alert;
public class UploadImage
{
private var urlRequest:URLRequest;
private var fileReferenceList:FileReferenceList;
private var serverSideScript:String = http://www.路徑/uploadImage.php; //處理上傳的指令檔路徑
[Bindable]
public var statusText:String; //記錄上傳檔案狀態
public function init():void
{
urlRequest = new URLRequest(serverSideScript); //串連上
fileReferenceList = new FileReferenceList();
fileReferenceList.addEventListener(Event.SELECT, fileSelectedHandler); //設定檔案選擇監聽
}
public function uploadFile():void
{
init();
fileReferenceList.browse(); //開啟瀏覽對話方塊
}
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(Event.COMPLETE, uploadCompleteHandler); //設定上傳過程結束監聽
fileReference.upload(urlRequest);
statusText = "上傳中...";
}
private function uploadCompleteHandler(event:Event):void //檔案上傳完畢監聽相應函數
{
statusText = "上傳成功的檔案: " + event.target.name;
}
}
}
uploadImage.php
<?php
$uploaddir = 'uploadImages/';
$uploadfile = $uploaddir . basename($_FILES['Filedata']['name']);
$temploadfile = $_FILES['Filedata']['tmp_name'];
move_uploaded_file($temploadfile , $uploadfile);
?>