热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->flex 
站内搜索:
使用Flex上传文件
作者:不详 来源:互联网 整理日期:2008-4-12

教程很简单, 先看看演示
http://kakera.35818.net/uploadsample/

客户端, 只有 1 个 mxml, 看注释就明
UploadSample.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
 layout="vertical" verticalAlign="middle" horizontalAlign="center">
 
 <mx:Style>
  global {
   fontSize : 12;
  }
 </mx:Style>
 
 <mx:Script>
  <![CDATA[
   // 先搞 1 个 FileReference
   private var file:FileReference = new FileReference();
   
   // 上传状态指示, 和下面的文本框绑定
   [Bindable]
   private var stateText:String = "请选择一个文件上传";
   
   // createChildren 比 creationComplete 事件更早发生, 省的注册事件侦听, 直接在这里写了
   protected override function createChildren():void {
    super.createChildren();
    file.addEventListener(Event.SELECT, file_select);
    file.addEventListener(Event.COMPLETE, file_complete);
    file.addEventListener(ProgressEvent.PROGRESS, file_progress);
   }
   
   // 选择 1 个文件的事件
   private function file_select (e:Event):void {
    stateText = "选择了文件 " + file.name;
   }
   
   // 上传完毕后的事件
   private function file_complete (e:Event):void {
    stateText = "上传完毕";
   }
   
   private function file_progress (e:ProgressEvent):void {
    stateText = "已上传 " + Math.round(100 * e.bytesLoaded / e.bytesTotal) + "%";
   }
   // 先判断一下文件大小, 再上传, FileService.aspx 就是上传地址
   private function upload ():void {
    if (file.size > 0) {
     stateText = "正在上传 " + file.name;
     var request:URLRequest = new URLRequest("FileService.aspx");
     file.upload(request);
    }
   }
   
   
  ]]>
 </mx:Script>
 
 <mx:Panel width="250" height="112" layout="vertical" title="上传示例"
  verticalAlign="middle" horizontalAlign="center" >
  <mx:HBox>
   <mx:TextInput text="{stateText}" width="160" editable="false"/>
   <mx:Button label="浏览" click="file.browse();"/>
  </mx:HBox>
  <mx:HBox>
   <mx:Button label="上传" click="upload();"/>
  </mx:HBox>
 </mx:Panel>
</mx:Application>

服务端, 使用 .net 制作, 看注释就明白,
看不懂的话也没关系只要知道文件上传到 upload 文件夹里就可以了 (要事先创建好), 文件名不变
FileService.aspx

<script language="C#" runat="server"> 
 string uploadFolder = "upload"; // 上传文件夹
 private void Page_Load(object sender, System.EventArgs e)
 {
  HttpFileCollection files = Request.Files;
  
  if (files.Count == 0)
  {
   Response.Write("请勿直接访问本文件");
   Response.End();
  }
  
  string path = Server.MapPath(uploadFolder);
  
  // 只取第 1 个文件
  HttpPostedFile file = files[0];
  
  if (file != null && file.ContentLength > 0)
  { 
   // flash 会自动发送文件名到 Request.Form["fileName"]
   string savePath = path + "/" + Request.Form["fileName"];
   file.SaveAs(savePath);
  }
 }
</script>

相关文章