<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" fontSize="12" creationComplete="initApp()">
<mx:Script>
<![CDATA[
/**
* Flex与php结合的上传功能
* Author:[url=http://www.phpcq.com]www.phpcq.com[/url] PHP技术博客
*/
import mx.controls.Text;
import mx.controls.Alert;
import flash.events.Event;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.FileFilter;
import flash.net.URLVariables;
import flash.events.ProgressEvent;
import mx.managers.CursorManager;
import mx.utils.StringUtil;
[Bindable]
private var file:FileReference;
private var fileName:String = "";
internal function initApp():void{
//FileReference 类提供了在用户计算机和服务器之间上载和下载文件的方法
file = new FileReference();
//选定文件后Event.SELECT事件触发
file.addEventListener(Event.SELECT,onSelect);
//获取上传进度
file.addEventListener(ProgressEvent.PROGRESS,progressHandler);
}
internal function selectFile():void{
//过滤文件
var imgType:FileFilter = new FileFilter("png","*.png");
var allType:Array = new Array(imgType);
//仅显示以上过滤后的文件
file.browse(allType);
}
internal function startUp():void{
var request:URLRequest =
new URLRequest("http://localhost/up/src/upload.php");//后台程序
fileName = new Date().getTime().toString()+".png";//文件重命名
request.data = new URLVariables("filename="+fileName);
if(StringUtil.trim(pic_txt.text) != ''){//当文件不能为空时
file.upload(request);
CursorManager.setBusyCursor();//设置鼠标为忙碌状态
} else {
Alert.show("上传文件不能为空!");
}
}
internal function onSelect(evt:Event):void{
pic_txt.text = file.name;//将文件名保存至文本框里
}
internal function progressHandler(e:ProgressEvent):void{
if(e.bytesLoaded == e.bytesTotal){//这里是至上传进度---上传成功后
CursorManager.removeBusyCursor();//取消忙碌状态
}
}
]]>
</mx:Script>
<!--文本框被事件触发为平时所见的文本域-->
<mx:TextInput x="235.5" y="177" height="21" id="pic_txt"
width="227" editable="false"/>
<mx:Button x="470.5" y="177" label="选择文件" click="selectFile()"/>
<mx:Button x="556.5" y="177" label="上传文件" click="startUp()"/>
<mx:Text x="173.5" y="178" text="上传文件" />
</mx:Application>
后台程序upload.php中的代码如下:
<?php
$imgName = $_GET['filename'];
$file = $_FILES['Filedata']['tmp_name'];
move_uploaded_file($file,$imgName);
本篇文章来源于PHP论坛 文章地址:http://bbs.php.cn/thread-41308-1-1.html
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute" fontSize="12" creationComplete="initApp()">
<mx:Script>
<![CDATA[
/**
* Flex与php结合的上传功能
* Author:[url=http://www.phpcq.com]www.phpcq.com[/url] PHP技术博客
*/
import mx.controls.Text;
import mx.controls.Alert;
import flash.events.Event;
import flash.net.FileReference;
import flash.net.URLRequest;
import flash.net.FileFilter;
import flash.net.URLVariables;
import flash.events.ProgressEvent;
import mx.managers.CursorManager;
import mx.utils.StringUtil;
[Bindable]
private var file:FileReference;
private var fileName:String = "";
internal function initApp():void{
//FileReference 类提供了在用户计算机和服务器之间上载和下载文件的方法
file = new FileReference();
//选定文件后Event.SELECT事件触发
file.addEventListener(Event.SELECT,onSelect);
//获取上传进度
file.addEventListener(ProgressEvent.PROGRESS,progressHandler);
}
internal function selectFile():void{
//过滤文件
var imgType:FileFilter = new FileFilter("png","*.png");
var allType:Array = new Array(imgType);
//仅显示以上过滤后的文件
file.browse(allType);
}
internal function startUp():void{
var request:URLRequest =
new URLRequest("http://localhost/up/src/upload.php");//后台程序
fileName = new Date().getTime().toString()+".png";//文件重命名
request.data = new URLVariables("filename="+fileName);
if(StringUtil.trim(pic_txt.text) != ''){//当文件不能为空时
file.upload(request);
CursorManager.setBusyCursor();//设置鼠标为忙碌状态
} else {
Alert.show("上传文件不能为空!");
}
}
internal function onSelect(evt:Event):void{
pic_txt.text = file.name;//将文件名保存至文本框里
}
internal function progressHandler(e:ProgressEvent):void{
if(e.bytesLoaded == e.bytesTotal){//这里是至上传进度---上传成功后
CursorManager.removeBusyCursor();//取消忙碌状态
}
}
]]>
</mx:Script>
<!--文本框被事件触发为平时所见的文本域-->
<mx:TextInput x="235.5" y="177" height="21" id="pic_txt"
width="227" editable="false"/>
<mx:Button x="470.5" y="177" label="选择文件" click="selectFile()"/>
<mx:Button x="556.5" y="177" label="上传文件" click="startUp()"/>
<mx:Text x="173.5" y="178" text="上传文件" />
</mx:Application>
后台程序upload.php中的代码如下:
<?php
$imgName = $_GET['filename'];
$file = $_FILES['Filedata']['tmp_name'];
move_uploaded_file($file,$imgName);
本篇文章来源于PHP论坛 文章地址:http://bbs.php.cn/thread-41308-1-1.html