Asp.net 2.0 用FileUpload 控件实现多文件上传用户控件(示例代码下载) |
发表时间:2007-8-27 15:36:00 |
(一). 示例代码 public partial class UpMultiFileControl2 : System.Web.UI.UserControl { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SaveCurrentPageFileControls(); } } protected void btAddFile_Click(object sender, EventArgs e) { AddOneFileControl(); } /** <summary> /// 添加一个上传文件控件 /// </summary> private void AddOneFileControl() { ArrayList al = new ArrayList(); this.tbFiles.Rows.Clear(); GetFileControlsFromSession(); HtmlTableRow htr = new HtmlTableRow(); HtmlTableCell htc = new HtmlTableCell(); htc.Controls.Add(new FileUpload()); htr.Controls.Add(htc); this.tbFiles.Rows.Add(htr); SaveCurrentPageFileControls(); } /** <summary> /// 读取缓存中存储的上传文件控件集 /// </summary> private void GetFileControlsFromSession() { ArrayList al = new ArrayList(); if (Session["FilesControls"] != null) { al = (System.Collections.ArrayList)Session["FilesControls"]; for (int i = 0; i < al.Count; i++) { HtmlTableRow htr1 = new HtmlTableRow(); HtmlTableCell htc1 = new HtmlTableCell(); htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]); htr1.Controls.Add(htc1); this.tbFiles.Rows.Add(htr1); } } } /** <summary> /// 保存当前页面上传文件控件集到缓存中 /// </summary> private void SaveCurrentPageFileControls() { ArrayList al = new ArrayList(); foreach (Control controlTR in this.tbFiles.Controls) { if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow") { HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0]; foreach (Control controlFileUpload in htc.Controls) { if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload") { FileUpload tempFileUpload = (FileUpload)controlFileUpload; al.Add(tempFileUpload); } } } } Session.Add("FilesControls", al); } protected void btUpFiles_Click(object sender, EventArgs e) { UpLoadFiles(); } /** <summary> /// 上传文件操作 /// </summary> private void UpLoadFiles() { string filepath = Server.MapPath("./")+"UploadFiles"; HttpFileCollection uploadedFiles = Request.Files; for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; try { if (userPostedFile.ContentLength > 0 ) { userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName)); Response.Write("已上传文件: \"" + filepath +"\\"+ userPostedFile.FileName +"\"<br><br>" ); } } catch { Response.Write("上传文件: \"" + userPostedFile.FileName +"\"出错!"); } } if (Session["FilesControls"] != null) { Session.Remove("FilesControls"); } } } (二). 改变上传文件大小和时间限制 <httpRuntime> executionTimeout="110"//允许上传文件最大等待时间 maxRequestLength="4096"//上传文件大小,默认为4M </httpRuntime> 上传文件大小是由上面两个参数所决定的. 涉及到安全因素,最好不要设得太大. (三). 示例源代码下载 http://www.cnblogs.com/Files/ChengKing/UpMultiFileControl.rar |
引用控件的aspx页面index2
<%@ Page Language="C#" AutoEventWireup="true" Inherits="index2" Codebehind="index2.aspx.cs" %>
<%@ Register Src="UpMultiFileControl2.ascx" TagName="UpMultiFileControl2" TagPrefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc1:UpMultiFileControl2 ID="UpMultiFileControl2_1" runat="server" />
</div>
</form>
</body>
</html>