FileReference 与URLRequest 上传图片后,如何获得服务器端返回值!
以下是FLEX 代码:
a.mxml
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init();">
<mx:Script>
<![CDATA[
import mx.controls.Alert;
import mx.managers.CursorManager;
private var urlRequset:URLRequest;
private var file:FileReference = new FileReference();//实例化一个文件类件
private var serverSide:String = "http://localhost/photo/upfile33.php";//指明后台处理的文件
private function init():void{
localChoose.addEventListener(MouseEvent.CLICK,doSelect);
file.addEventListener(Event.SELECT,onSelect);
resetBtn.addEventListener(MouseEvent.CLICK,doReset);
submitBtn.addEventListener(MouseEvent.CLICK,doUpload);
}
public function uploadCompleteHandler(event:Event):void{//上传成功
localPath.text = event.target.name+"文件上传成功!";
Alert.show(localPath.text,"提示");
}
public function doSelect(evt:Event):void{//选取文件
file.browse();
}
public function onSelect(evt:Event):void{//选取文件完成
localPath.text=file.name;
}
public function doReset(evt:Event):void{//重置
localPath.text='';
}
public function doUpload(event:Event):void{//开始上传
if(localPath.text==''){
Alert.okLabel = "确 定";
Alert.show("请选择要上传的文件!","警告");
}else{
var request:URLRequest=new URLRequest(serverSide);
file.addEventListener(Event.COMPLETE, uploadCompleteHandler);
file.addEventListener(ProgressEvent.PROGRESS,processHandler);
localPath.text = "正在上传 " + file.name;
CursorManager.setBusyCursor();
file.upload(request);
}
}
public function processHandler(evt:ProgressEvent):void{//上传进度
if(evt.bytesLoaded == evt.bytesTotal)
{
CursorManager.removeBusyCursor();
}else
{
var proc:uint=evt.bytesLoaded/evt.bytesTotal*100;
localPath.text="已上传:"+proc;
}
}
]]>
</mx:Script>
<mx:Panel x="10" y="10" width="421" height="184" layout="absolute" title="FLEX上传工具DEMO" fontSize="12" fontWeight="bold">
<mx:Label x="37" y="31" text="上传文件:"/>
<mx:TextInput id="localPath" x="107" y="29" width="179.5" editable="false"/>
<mx:Button id="localChoose" x="312" y="29" label="选择"/>
<mx:Button id="submitBtn" x="107" y="79" label="开始上传" />
<mx:Button id="resetBtn" x="234.5" y="79" label="重填"/>
</mx:Panel>
</mx:Application>
以下是:http://localhost/photo/upfile33.php
<?php
$uploaddir = 'uploads/';
$filename = date("Ymdhis").rand(100,999);//上传改后文件名
$uploadfile = $uploaddir .$filename.substr($_FILES['Filedata']["name"],strrpos($_FILES['Filedata']["name"],"."));
$temploadfile = $_FILES['Filedata']['tmp_name'];
file_put_contents("catcah.txt",$_FILES['Filedata']['tmp_name']);
move_uploaded_file($temploadfile , $uploadfile);
echo $filename; //这里是FLEX 需要接收的返回值 也就是图片新名称。
?>