网上找了一个用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);
?>