Flex上传单个文件的源码如下:
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="appComplete()">
<fx:Declarations>
<!-- 将非可视元素(例如服务、值对象)放在此处 -->
</fx:Declarations>
<fx:Script>
<![CDATA[
import mx.events.CloseEvent;
import flash.net.FileReference;
import mx.controls.Alert;
private var fileReference:FileReference;
private var urlRequest:URLRequest;
private function appComplete():void{
fileReference = new FileReference();
fileReference.addEventListener(Event.SELECT, onSelect);
fileReference.addEventListener(Event.COMPLETE,onComplete);
urlRequest = new URLRequest("http://localhost:8666/Ashx/FileHandler.ashx");
}
//上传
private function browse():void{
var imageTypes:FileFilter = new FileFilter("图片 (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg; *.jpeg; *.gif; *.png");
var allTypes:Array = new Array(imageTypes);
try
{
fileReference.browse(allTypes);
}
catch(e:Error)
{
Alert.show(e.message+"&&"+e.getStackTrace());
}
}
//文件被选择后调度
private function onSelect(evt:Event):void{
labelState.text="文件:"+fileReference.name+" "+"大小:"+fileReference.size+"字节";
fileReference.load();
}
//fileReference.load()运行成功后调度
private function onComplete(evt:Event):void{
image.source=fileReference.data;
}
//确认上传警告
private function upload():void{
fileReference.addEventListener(ProgressEvent.PROGRESS,onPROGRESS);
fileReference.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUPLOAD_COMPLETE_DATA);
Alert.show("上传?","确认",Alert.YES|Alert.NO,null,proceedWithUpload);
}
//执行上传
private function proceedWithUpload(evt:CloseEvent):void{
if(evt.detail == Alert.YES){
fileReference.upload(urlRequest);
}
}
//侦听上传进度
private function onPROGRESS(evt:ProgressEvent):void{
labelState.text = " 已上传 " + evt.bytesLoaded + " 字节,共 " + evt.bytesTotal + " 字节";
fileReference.removeEventListener(ProgressEvent.PROGRESS,onPROGRESS);
}
//侦听成功上传
private function onUPLOAD_COMPLETE_DATA(evt:DataEvent):void
{
if(evt.data == " Success"){
Alert.show("上传成功");
}
fileReference.removeEventListener(DataEvent.UPLOAD_COMPLETE_DATA,onUPLOAD_COMPLETE_DATA);
}
]]>
</fx:Script>
<mx:Button label="浏览文件" click="browse()"/>
<mx:Button label="上传" click="upload()" x="78"/>
<mx:Label id="labelState" y="30" width="369" height="19"/>
<mx:Image id="image" visible="true" x="50" y="138" width="150" height="180"/>
</s:Application>
其中处理上传并保存的那个地址所对应的页面的处理过程如下("http://localhost:8666/Ashx/FileHandler.ashx"):
public class FileHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpPostedFile FileData = context.Request.Files["Filedata"];
string result = "";
try
{
// result = Path.GetFileName(FileData.FileName);//获得文件名
string ext = Path.GetExtension(FileData.FileName);//获得文件扩展名
string newFileName=Guid.NewGuid().ToString();
string saveName =newFileName + ext;//实际保存文件名
saveFile(FileData, context.Request.MapPath("~" + context.Request["folder"] + "/"), saveName);//保存文件
result = saveName;
}
catch (Exception ex)
{
result = "";
}
context.Response.ContentType = "text/plain";
context.Response.Write(result);
}
private void saveFile(HttpPostedFile postedFile, string phyPath, string saveName)
{
if (!Directory.Exists(phyPath))
{
Directory.CreateDirectory(phyPath);
}
try
{
postedFile.SaveAs(phyPath + saveName);
}
catch (Exception e)
{
throw new ApplicationException(e.Message);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}