带页面返回值处理的
<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"
    creationComplete="init()" xmlns:local="*">
 
 <fx:Script>
  <![CDATA[
   
  private const defaultRequestUrl : String = "http://192.168.0.212:8002/Talk/UploadHandler.ashx";
  
  private var file : FileReference;
  
  private function init():void {
   Security.allowDomain("*");
   
   file = new FileReference();
   file.addEventListener(Event.SELECT, onFileSelect);
   file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
   file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, completeHandle);
   //file.addEventListener(Event.OPEN, openHandle);
   //file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
   //file.addEventListener(Event.CANCEL, cancelHandler);
  }
  
  private function onClickBrowserBtn() : void {
   file.browse(getTypeFilter());
  }
  
  private function getTypeFilter() : Array {
   var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
   //var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
   
   return [imagesFilter];
  }
  
  private function onFileSelect(event : Event) : void {
   uploadBtn.enabled = true;
   infoText.htmlText =
     "Name: " + file.name + "<br/>" +
     "Size: " + file.size + "<br/>" +
     "Type: " + file.type + "<br/>" +
     "Date: " + file.creationDate;
  }
  
  private function onClickUploadBtn() : void {
   var request : URLRequest = new URLRequest(defaultRequestUrl);
   request.data = "userId=123";
   file.upload(request);
  }

private function progressHandle(event : ProgressEvent) : void {
   progressLabel.text = "complete " + event.bytesLoaded + " bytes";
   var fileUploadPercent : uint = event.bytesLoaded / event.bytesTotal * 100;
   uploadProgressBar.setProgress(fileUploadPercent, 100);
   uploadProgressBar.label = "Complete " + fileUploadPercent + "%";
  }
  
  private function completeHandle(event : DataEvent) : void {
   infoText.htmlText = "Upload " + file.name + " Complete!<br>"+event.data;
   uploadBtn.enabled = false;
  }
   
  ]]>
 </fx:Script>
 
 <mx:Button id="browserBtn" x="10" y="69" label="Browser"
      click="onClickBrowserBtn()"/>
 
 <mx:Button id="uploadBtn" x="236" y="69" label="Upload" enabled="false"
      click="onClickUploadBtn()"/>
 
 <mx:ProgressBar id="uploadProgressBar" x="10" y="33" width="291"
     maximum="100" direction="right" mode="manual"/>
 
 <mx:TextArea id="infoText" x="10" y="99" width="291" height="131"/>
 <mx:Label id="progressLabel" x="10" y="10" width="291"/>
</s:Application>

示例1:
<?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" xmlns="*" creationComplete="init();">  
 <fx:Script>  
  <![CDATA[ 
  import flash.net.FileReference;  
  import mx.controls.Alert;  
  import mx.events.CloseEvent;  
  import flash.events.*;  
  
  private var file: FileReference;  
  
  private function init(): void{  
   Security.allowDomain("*");  
   file = new FileReference();  
   file.addEventListener(ProgressEvent.PROGRESS, onProgress);  
   file.addEventListener(Event.SELECT, onSelect);  
  }  
  
  private function upload(): void{  
   file.browse();  
  }  
  
  private function onSelect(e: Event): void{  
   Alert.show("上传 " + file.name + " (共 "+Math.round(file.size)+" 字节)?",  
    "确认上传",  
    Alert.YES|Alert.NO,  
    null,  
    proceedWithUpload);  
  }  
  
  private function onProgress(e: ProgressEvent): void{  
   lbProgress.text = " 已上传 " + e.bytesLoaded   
    + " 字节,共 " + e.bytesTotal + " 字节";  
   var proc: uint = e.bytesLoaded / e.bytesTotal * 100;  
   bar.setProgress(proc, 100);  
   bar.label= "当前进度: " + " " + proc + "%";  
  }  
  
  private function proceedWithUpload(e: CloseEvent): void{  
   if (e.detail == Alert.YES){  
    var request: URLRequest = new URLRequest("http://192.168.0.212:8002/Talk/UploadHandler.ashx");  
    try {  
     file.upload(request);  
    } catch (error:Error) {  
     trace("上传失败");  
    }  
    
   }  
  }  
   ]]>  
  </fx:Script>  
    
  <mx:Canvas width="100%" height="100%">  
   <mx:VBox width="100%" horizontalAlign="center">  
    <mx:Label id="lbProgress" text="上传"/>  
    <mx:ProgressBar id="bar" labelPlacement="bottom"
        minimum="0" visible="true" maximum="100" label="当前进度: 0%"     
        direction="right" mode="manual" width="200"/>  
    <mx:Button label="上传文件" click="upload();"/>               
   </mx:VBox>  
  </mx:Canvas>  
</s:Application>

示例2:
<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"
    creationComplete="init()">
 
 <fx:Script>
  <![CDATA[
   
   private const defaultRequestUrl : String = "http://192.168.0.212:8002/Talk/UploadHandler.ashx";
   
   private var file : FileReference;
  
  private function init():void {
   Security.allowDomain("*");
   
   file = new FileReference();
   file.addEventListener(Event.SELECT, onFileSelect);
   file.addEventListener(ProgressEvent.PROGRESS, progressHandle);
   file.addEventListener(Event.COMPLETE, completeHandle);
   //file.addEventListener(Event.OPEN, openHandle);
   //file.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
   //file.addEventListener(Event.CANCEL, cancelHandler);
  }
  
  private function onClickBrowserBtn() : void {
   file.browse(getTypeFilter());
  }
  
  private function getTypeFilter() : Array {
   var imagesFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
   //var docFilter:FileFilter = new FileFilter("Documents", "*.pdf;*.doc;*.txt");
   
   return [imagesFilter];
  }
  
  private function onFileSelect(event : Event) : void {
   uploadBtn.enabled = true;
   infoText.htmlText =
     "Name: " + file.name + "<br/>" +
     "Size: " + file.size + "<br/>" +
     "Type: " + file.type + "<br/>" +
     "Date: " + file.creationDate;
  }
  
  private function onClickUploadBtn() : void {
   var request : URLRequest = new URLRequest(defaultRequestUrl);
   request.data = "userId=123";
   file.upload(request);
  }
  
  private function progressHandle(event : ProgressEvent) : void {
   progressLabel.text = "complete " + event.bytesLoaded + " bytes";
   var fileUploadPercent : uint = event.bytesLoaded / event.bytesTotal * 100;
   uploadProgressBar.setProgress(fileUploadPercent, 100);
   uploadProgressBar.label = "Complete " + fileUploadPercent + "%";
  }
  
  private function completeHandle(event : Event) : void {
   infoText.htmlText = "Upload " + file.name + " Complete!";
   uploadBtn.enabled = false;
  }
  ]]>
 </fx:Script>
 
 <mx:Button id="browserBtn" x="10" y="69" label="Browser"
      click="onClickBrowserBtn()"/>
 
 <mx:Button id="uploadBtn" x="236" y="69" label="Upload" enabled="false"
      click="onClickUploadBtn()"/>
 
 <mx:ProgressBar id="uploadProgressBar" x="10" y="33" width="291"
     maximum="100" direction="right" mode="manual"/>
 
 <mx:TextArea id="infoText" x="10" y="99" width="291" height="131"/>
 <mx:Label id="progressLabel" x="10" y="10" width="291"/>
 
</s:Application>

.net后台

/// <summary>
    /// UploadHandler 的摘要说明
    /// </summary>
    public class UploadHandler : IHttpHandler
    {

public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Response.Charset = "utf-8";

HttpPostedFile file = context.Request.Files["Filedata"];

if (file != null)
            {
                string newFilePath = context.Server.MapPath("~/") + "/files/" + DateTime.Now.ToString("yyyy") + "\\" + DateTime.Now.ToString("MM") + "\\" + DateTime.Now.ToString("dd") + "\\";
                Directory.CreateDirectory(newFilePath);
                string newFileName = DateTime.Now.Ticks.ToString() + file.FileName.Substring(file.FileName.LastIndexOf('.'));
                file.SaveAs(newFilePath + newFileName);
                string url = "/" + DateTime.Now.ToString("yyyy") + "/" + DateTime.Now.ToString("MM") + "/" + DateTime.Now.ToString("dd") + "/" + newFileName;
                //context.Session["MovieFile"] = new PFileInfo(file.FileName, file.ContentLength, url, file.FileName.Substring(file.FileName.LastIndexOf('.')));
                context.Response.Write(url);
            }
            else
            {
                context.Response.Write("");
            }
        }

public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

flex上传文件代码相关推荐

  1. php+easyui+上传文件,easyui 上传文件代码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO ...

  2. form表单提交数据的同时上传文件代码示例

    form表单提交数据的同时在表单中上传文件代码示例 一.定义页面 注意:在form表单中加入属性 enctype="multipart/form-data"  表示此表单支持文件上 ...

  3. Git上传文件代码到GitHub

    Git上传文件代码到GitHub 1. 新建一个空文件夹,用来上传文件 2. 点进去空文件夹,鼠标右键,使用Git Bash Here 打开 3. 输入 git init ,初始化,在本地创建一个Gi ...

  4. java flex 上传文件_使用Flex和java servlet上传文件

    资源都是来自网上.本实例将展示使用Flex和java servlet上传文件. 事前准备就是到http://commons.apache.org 下载common-fileupload-1.1.1.j ...

  5. 往Domin 里上传文件代码

    fileURL:上传文件的路地址 docfile:byte[]类型的字节流 private void UploadToLibrary(string fileURL, byte[] docfile)   ...

  6. Hadoop hdfs 使用流来上传文件代码示例

  7. [moka同学代码]PHP初级知识:上传文件源码

    1.目录结构 2.index.php <html> <head> <meta charset="utf-8"> <title>上传文 ...

  8. 在WinForm中通过HTTP协议向服务器端上传文件(转)

    相信用ASP.NET写一个上传文件的网页,大家都会写,但是有没有人想过通过在WinForm中通过HTTP协议上传文件呢? 有些人说要向服务器端上传文件,用FTP协议不是很简单吗?效率又高,为什么还要使 ...

  9. C# winform 上传文件 (多种方案)

    方案一: 注意:要开启虚拟目录的"写入"权限,要不然就报 403 错误 工作中用到winform上传文件(-_-!,很少用winform,搞了半天) 碰到一点问题,解决如下 1.5 ...

最新文章

  1. 久坐 缺乏运动 消化能力 会减弱
  2. centos7编译安装pure-ftpd-1.0.42
  3. “保持耐心”,永远从用户角度出发— 专访阿里巴巴淘系技术内容中台负责人吴桂林(梁舒)...
  4. mybatis学习(48):列表信息查询
  5. php隐藏路径ngnix,thinkphp框架在nginx环境下去掉index.php路径显示
  6. 3d 仪表盘_新一代标致2008官图发布 配备3D全息仪表盘
  7. Jquery实现列表框效果
  8. springmvc流程_基于Spring MVC框架的Http流程分析
  9. dos创建mysql数据库_用命令创建MySQL数据库
  10. 『中级篇』docker之CI/CD持续集成-(终结篇)(77)
  11. C语言之数组为参数传递表示指针(三十七)
  12. Apache下的Lua的配置
  13. 垃圾分类-特别是有害垃圾
  14. 使用jquery对接高德地图地址四级联动
  15. deepin自己更新火狐esr
  16. 装满了自我提升的33个学习平台
  17. 视频原理和FFmpeg
  18. 在Robot FrameWork中引用自定义关键字的过程
  19. 从零教你用抖音赚钱——吸粉变现技巧
  20. Chino with Rewrite

热门文章

  1. 前端学习(1809):前端调试之微博头部开发
  2. 前端学习(1773):前端调试之快速清空所有的本地存储资源
  3. 前端学习(82):按内容进行分类
  4. mybatis学习(3):映射文件的配置和接口创建
  5. 第十五期:详解Java集合框架,让你全面掌握!
  6. HardFault_Handler问题查找方法
  7. html文字置顶标签,HTML的marquee标签怎么用?
  8. IP包的生成和发送接口(1)
  9. mysql问题处理积累
  10. POJ1459 Power Network —— 最大流