Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)

2024-02-24 14:38

本文主要是介绍Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

学习, And 整理了一下.

(一). 示例图片

     

(二). 示例代码

 

  1 public  partial  class  UpMultiFileControl2 : System.Web.UI.UserControl
  2 {
  3    protected void Page_Load(object sender, EventArgs e)
  4    {
  5        if (!Page.IsPostBack)
  6        {
  7            SaveCurrentPageFileControls();
  8        }

  9    }

 10    protected void btAddFile_Click(object sender, EventArgs e)
 11    {
 12        AddOneFileControl();
 13    }

 14
 15    /// <summary>
 16    /// 添加一个上传文件控件
 17    /// </summary>

 18    private void AddOneFileControl()
 19    {
 20        ArrayList al = new ArrayList();
 21        this.tbFiles.Rows.Clear();
 22        GetFileControlsFromSession();        
 23        HtmlTableRow htr = new HtmlTableRow();
 24        HtmlTableCell htc = new HtmlTableCell();        
 25        htc.Controls.Add(new FileUpload());
 26        htr.Controls.Add(htc);
 27        this.tbFiles.Rows.Add(htr);
 28        SaveCurrentPageFileControls();
 29    }

 30
 31    /// <summary>
 32    /// 读取缓存中存储的上传文件控件集
 33    /// </summary>

 34    private void GetFileControlsFromSession()
 35    {
 36        ArrayList al = new ArrayList();       
 37        if (Session["FilesControls"!= null)
 38        {
 39            al = (System.Collections.ArrayList)Session["FilesControls"];
 40            for (int i = 0; i < al.Count; i++)
 41            {
 42                HtmlTableRow htr1 = new HtmlTableRow();                
 43                HtmlTableCell htc1 = new HtmlTableCell();
 44                htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);
 45                htr1.Controls.Add(htc1);
 46                this.tbFiles.Rows.Add(htr1);
 47            }

 48        }

 49    }

 50    
 51    /// <summary>
 52    /// 保存当前页面上传文件控件集到缓存中
 53    /// </summary>    

 54    private void SaveCurrentPageFileControls()
 55    {        
 56        ArrayList al = new ArrayList();        
 57        foreach (Control controlTR in this.tbFiles.Controls)
 58        {
 59            if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
 60            {
 61                HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];
 62                foreach (Control controlFileUpload in htc.Controls)
 63                {
 64                    if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
 65                    {
 66                        FileUpload tempFileUpload = (FileUpload)controlFileUpload;
 67                        al.Add(tempFileUpload);
 68                    }

 69                }

 70            }
            
 71        }
  
 72        Session.Add("FilesControls", al);
 73    }

 74
 75    protected void btUpFiles_Click(object sender, EventArgs e)
 76    {
 77        UpLoadFiles();
 78    }

 79
 80    /// <summary>
 81    /// 上传文件操作
 82    /// </summary>

 83    private void UpLoadFiles()
 84    {
 85        string filepath = Server.MapPath("./")+"UploadFiles";
 86        
 87        HttpFileCollection uploadedFiles = Request.Files;       
 88        for (int i = 0; i < uploadedFiles.Count; i++)
 89        {    
 90           HttpPostedFile userPostedFile = uploadedFiles[i];        
 91           try
 92           {    
 93              if (userPostedFile.ContentLength > 0 )
 94              {  
 95                 userPostedFile.SaveAs(filepath + "//" + System.IO.Path.GetFileName(userPostedFile.FileName));
 96                 Response.Write("已上传文件: /"" + filepath +"//"+ userPostedFile.FileName +"/"<br><br>" );                                   
 97              }
    
 98           }
 
 99           catch
100           {
101               Response.Write("上传文件: /"" + userPostedFile.FileName +"/"出错!");
102           }
    
103       }

104       if (Session["FilesControls"!= null)
105       {
106           Session.Remove("FilesControls");
107       }

108    }
    
109}

 

(三). 改变上传文件大小和时间限制

       <httpRuntime>
            executionTimeout="110"              //允许上传文件最大等待时间
            maxRequestLength="4096"        //上传文件大小,默认为4M
       </httpRuntime>

       上传文件大小是由上面两个参数所决定的.  涉及到安全因素,最好不要设得太大.      

(四). 示例源代码下载

       http://www.cnblogs.com/Files/ChengKing/UpMultiFileControl.rar



Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=699244

 

这篇关于Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/742428

相关文章

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

MySQL常用字符串函数示例和场景介绍

《MySQL常用字符串函数示例和场景介绍》MySQL提供了丰富的字符串函数帮助我们高效地对字符串进行处理、转换和分析,本文我将全面且深入地介绍MySQL常用的字符串函数,并结合具体示例和场景,帮你熟练... 目录一、字符串函数概述1.1 字符串函数的作用1.2 字符串函数分类二、字符串长度与统计函数2.1

Redis客户端连接机制的实现方案

《Redis客户端连接机制的实现方案》本文主要介绍了Redis客户端连接机制的实现方案,包括事件驱动模型、非阻塞I/O处理、连接池应用及配置优化,具有一定的参考价值,感兴趣的可以了解一下... 目录1. Redis连接模型概述2. 连接建立过程详解2.1 连php接初始化流程2.2 关键配置参数3. 最大连

Python实现网格交易策略的过程

《Python实现网格交易策略的过程》本文讲解Python网格交易策略,利用ccxt获取加密货币数据及backtrader回测,通过设定网格节点,低买高卖获利,适合震荡行情,下面跟我一起看看我们的第一... 网格交易是一种经典的量化交易策略,其核心思想是在价格上下预设多个“网格”,当价格触发特定网格时执行买

python设置环境变量路径实现过程

《python设置环境变量路径实现过程》本文介绍设置Python路径的多种方法:临时设置(Windows用`set`,Linux/macOS用`export`)、永久设置(系统属性或shell配置文件... 目录设置python路径的方法临时设置环境变量(适用于当前会话)永久设置环境变量(Windows系统

最新Spring Security的基于内存用户认证方式

《最新SpringSecurity的基于内存用户认证方式》本文讲解SpringSecurity内存认证配置,适用于开发、测试等场景,通过代码创建用户及权限管理,支持密码加密,虽简单但不持久化,生产环... 目录1. 前言2. 因何选择内存认证?3. 基础配置实战❶ 创建Spring Security配置文件

Python对接支付宝支付之使用AliPay实现的详细操作指南

《Python对接支付宝支付之使用AliPay实现的详细操作指南》支付宝没有提供PythonSDK,但是强大的github就有提供python-alipay-sdk,封装里很多复杂操作,使用这个我们就... 目录一、引言二、准备工作2.1 支付宝开放平台入驻与应用创建2.2 密钥生成与配置2.3 安装ali

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

SQL Server 中的 WITH (NOLOCK) 示例详解

《SQLServer中的WITH(NOLOCK)示例详解》SQLServer中的WITH(NOLOCK)是一种表提示,等同于READUNCOMMITTED隔离级别,允许查询在不获取共享锁的情... 目录SQL Server 中的 WITH (NOLOCK) 详解一、WITH (NOLOCK) 的本质二、工作

PyCharm中配置PyQt的实现步骤

《PyCharm中配置PyQt的实现步骤》PyCharm是JetBrains推出的一款强大的PythonIDE,结合PyQt可以进行pythion高效开发桌面GUI应用程序,本文就来介绍一下PyCha... 目录1. 安装China编程PyQt1.PyQt 核心组件2. 基础 PyQt 应用程序结构3. 使用 Q