惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

人人都是产品经理
人人都是产品经理
宝玉的分享
宝玉的分享
小众软件
小众软件
有赞技术团队
有赞技术团队
月光博客
月光博客
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
Stack Overflow Blog
Stack Overflow Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
N
Netflix TechBlog - Medium
D
Docker
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
MongoDB | Blog
MongoDB | Blog
WordPress大学
WordPress大学
J
Java Code Geeks
罗磊的独立博客
V
Visual Studio Blog
雷峰网
雷峰网
H
Help Net Security
T
The Blog of Author Tim Ferriss
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
F
Fortinet All Blogs

博客园 - dannyr|一个都不能少!

DipperRiver.Net通信协议设计 计划开发memcache 的.net版本,命名DipperRiver.Net Net1.1添加目录共享,并设置访问权限 Access数据库的文本、备注数据类型的COLUMN_FLAGS说明 生命诚可贵 如何关闭子线程?征集析构函数与多线程的讨论! function object(functor) ... Dev GridControl的Outlook风格定制 WinForm MDI动态加载form DevExpress's tip 检测浏览器类型的js Spry1.4 下载 Spry PreRelease 1.4 发布 Spry Framework入门(五)——数据集过滤及淡入淡出效果 关于JSON Spry Framework入门(四)——XML数据集排序 Spry Framework入门(三)——框架结构 Spry Framework入门(二)——XML数据集及主从表显示 Spry Framework入门(一)——XML数据集及显示
Flex2.0文件上传功能(Flex2.0正式版)
dannyr|一个都不能少! · 2006-11-13 · via 博客园 - dannyr|一个都不能少!

简介
        新的Flex2.0类库里提供了文件类,方便了上传/下载文件。下面的程序demo演示了Flex2.0生成flash来访问本地文件,在flash里上传用户选择的文件到服务器,flash客户端可以处理文件上传进度等多个事件,服务器端是C#写的文件接收模块,把用户上传的文件保存在服务器上。
        Demo演示了ProgressEvent.PROGRESS, Event.SELECT 2个事件的处理方法。

        顺便提一下关于JSP的接收Flex上传文件的方法(很多网友问过这个问题),我建议使用Jakarta Commons FileUpload的文件上传组件,详见:http://jakarta.apache.org/commons/fileupload/

测试效果

flexupload1.jpg

flexupload2.jpg

测试环境

操作系统:windows2003 Server
Flex版本:Flex 2.0
Flash版本:

flash Player 9
WEB服务器:
          IIS 6.0
         .net FrameWork 1.1

客户端代码:FileUpload.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
    xmlns
="*" creationComplete="init();">
    
<mx: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://localhost/JZService/WebForm1.aspx");
                    try {
                        file.upload(request);
                    } catch (error:Error) {
                        trace("上传失败");
                    }
                    
                }
            }
        
]]>
    
</mx:Script>
    
    
<mx:Canvas width="100%" height="100%">
        
<mx:VBox width="100%" horizontalAlign="center">
            
<mx:Label id="lbProgress" text="上传"/>
             
<mx:ProgressBar id="bar" labelPlacement="bottom" themeColor="#F20D7A"
                minimum
="0" visible="true" maximum="100" label="当前进度: 0%"  
                direction
="right" mode="manual" width="200"/>
            
<mx:Button label="上传文件" click="upload();"/>            
        
</mx:VBox>
    
</mx:Canvas>
</mx:Application>

服务端代码:WebForm1.aspx

        private void Page_Load(object sender, EventArgs e) {
            
// 在此处放置用户代码以初始化页面
            HttpFileCollection uploadedFiles =  Request.Files;
            
string Path = Server.MapPath("data");
            
for(int i = 0 ; i < uploadedFiles.Count ; i++{
                HttpPostedFile F 
= uploadedFiles[i];
                
if(uploadedFiles[i] != null && F.ContentLength > 0{   
                    
string newName = F.FileName.Substring(F.FileName.LastIndexOf("\\"+ 1);
                    F.SaveAs(Path 
+ "/" + newName);
                }

            }


        }