1 Flex代码
<script> import flash.net.FileReference; <br> import mx.controls.Alert; <br> import mx.events.CloseEvent; <br> import flash.events.*; <br> private var file : FileReference; <br> private var uploadURL : URLRequest; <br> private function init() : void{ <br> Security.allowDomain("*"); <br> file = new FileReference(); <br> file.addEventListener(ProgressEvent.PROGRESS, onProgress); <br> file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUPLOAD_COMPLETE_DATA); <br> file.addEventListener(Event.SELECT, onSelect); <br> uploadURL = new URLRequest(); <br> uploadURL.url = "http://localhost:80/ShowProduct-debug/uploadFile.php"; <br> uploadURL.method = "post"; <br> } <br> private function upload() : void{ <br> var imageTypes:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png"); <br> var allTypes:Array = new Array(imageTypes); <br> file.browse(allTypes); <br> } <br> private function onSelect(e : Event) : void{ <br> Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?", "确认上传",Alert.YES|Alert.NO,null,proceedWithUpload); <br> } <br> private function onProgress(e:ProgressEvent) : void{ <br> lbProgress.text = " 已上传 " + e.bytesLoaded + " 字节,共 " + e.bytesTotal + " 字节"; <br> } <br> private function proceedWithUpload(e : CloseEvent) : void{ <br> if (e.detail == Alert.YES){ <br> file.upload(uploadURL); <br> } <br> } <br> private function onUPLOAD_COMPLETE_DATA(evt:DataEvent):void <br> { <br> Alert.show(evt.data.toString()); <br> } <br> ]]> <br> </script>
注:uploadURL.url = "http://localhost:80/ShowProduct-debug/uploadFile.php";路径一定要写对,否则回报Error #2044: 未处理的 IOErrorEvent:。 text=Error #2038: 文件 I/O 错误。
2. php代码
// Flash 传递的文件表单 name 属性为 Filedata
$fileName = $_FILES["Filedata"]["name"];
$file = $_FILES["Filedata"]["tmp_name"];
$path = "uploadFiles/";
if(!is_dir("./uploadFiles")){
@mkdir("./uploadFiles");
}
if (move_uploaded_file($file, $path . $fileName)){
echo Success;
}else{
echo Fail;
}
?>
注:php文件一定要放到Apache运行的目录里,例如:把php文件放到C:\xampp\htdocs\ShowProduct-debug目录下