先看看效果图:
今天做了个简单的多文件上传的效果,效果如上,可以多文件上传......
那就看看我是如何实现的吧!里面有详细的注释......供大家参考,不足的地方希望大家指出,加以改正......
1.UploadFile.aspx 展示页面
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadFile.aspx.cs" Inherits="MoreFileUploadPrj._Default" %>
<!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>多文件上传</title>
<style type="text/css">
body
{
font-family: "宋体";
font-size: 13px;
color: #000;
margin-left: 0px;
margin-top: 0px;
margin-right: 0px;
margin-bottom: 0px;
background-color: #f7f7ee;
}
.b
{
font-family: "宋体";
font-size: 13px;
color: #000;
font-weight: bold;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div align="center">
<table border="0" cellpadding="0" cellspacing="0" style="width: 50%; border-style: solid;
border-color: Red;">
<tr>
<td align="center" colspan="3" height="40px">
<span class="b">上传图片</span>
</td>
</tr>
<tr>
<td align="right" height="35px">
图片路径:
</td>
<td align="left" colspan="2" style="width: 500px">
<table id="tab_FileUpload_Area" runat="server" style="width: 120px" cellpadding="0"
cellspacing="0">
<tr>
<td>
<asp:FileUpload ID="fluPic" runat="server" />
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td align="center" colspan="3" height="60px">
<asp:ImageButton ID="imgBtnAdd" runat="server" ImageUrl="Images/zjtp.jpg" OnClick="imgBtnAdd_Click" />
<asp:ImageButton ID="imgBtnUploadPic" runat="server" ImageUrl="Images/sctp.jpg" OnClick="imgBtnUploadPic_Click" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
2.UploadFile.aspx.cs 后台代码部分
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.IO;//添加此命名空间
namespace MoreFileUploadPrj
{
public partial class _Default : System.Web.UI.Page
{
#region Attribute
#endregion
#region Page_Load
/// <summary>
/// 页面加载事件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetFileUpload();//第一次点击时也可以添加一个上传控件
}
}
#endregion
#region Function
/// <summary>
/// 添加一个上传文件的控件(FileUpload)
/// </summary>
private void AddFileUpload()
{
//清除Table中的行
this.tab_FileUpload_Area.Rows.Clear();
//调用自定义方法,获取上传控件集
GetFileUpload();
HtmlTableRow tabRow = new HtmlTableRow();
HtmlTableCell tabCell = new HtmlTableCell();
//添加上传控件
tabCell.Controls.Add(new FileUpload());
tabRow.Cells.Add(tabCell);
this.tab_FileUpload_Area.Rows.Add(tabRow);
//调用自定义方法,保存控件集到缓存中
SetFileUpload();
}
/// <summary>
/// 获取缓存中上传控件集
/// </summary>
private void GetFileUpload()
{
//声明数据对象
ArrayList arrayList = new ArrayList();
if (Session["FilesControls"] != null)
{
//将生成的上传控件,显示在Table表格中
arrayList = (ArrayList)Session["FilesControls"];
for (int i = 0; i < arrayList.Count; i++)
{
HtmlTableRow tabRow = new HtmlTableRow();
HtmlTableCell tabCell = new HtmlTableCell();
tabCell.Controls.Add((FileUpload)arrayList[i]);
tabRow.Cells.Add(tabCell);
this.tab_FileUpload_Area.Rows.Add(tabRow);
}
}
}
/// <summary>
/// 将当前页面中的上传控件集保存到缓存中去
/// </summary>
private void SetFileUpload()
{
//创建动态数组
ArrayList arrayList = new ArrayList();
foreach (Control c in tab_FileUpload_Area.Controls)
{
//判断控件类型
if (c.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell tabCell = (HtmlTableCell)c.Controls[0];
//Table单元格中检索FileUpload控件
foreach (Control control in tabCell.Controls)
{
//判断控件类型
if (control.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
//将FileUpload控件信息保存到ArrayList控件中
FileUpload f = (FileUpload)control;
arrayList.Add(f);
}
}
}
}
Session.Add("FilesControls", arrayList);
}
#endregion
#region Event
/// <summary>
/// 添加上传控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void imgBtnAdd_Click(object sender, ImageClickEventArgs e)
{
//调用添加上传控件
AddFileUpload();
}
/// <summary>
/// 上传图片到指定文件夹下
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void imgBtnUploadPic_Click(object sender, ImageClickEventArgs e)
{
//设置上传图片保存的路径
string FilePath = Server.MapPath("./") + "UploadFile";
//获取上传图片的集合
HttpFileCollection Hfc = Request.Files;
for (int i = 0; i < Hfc.Count; i++)
{
HttpPostedFile Hpf=Hfc[i];
if(Hpf.ContentLength>0)
{
//获取上传图片的文件名
string FileName = Hpf.FileName.Substring(Hpf.FileName.LastIndexOf("\\"));
//保存图片到指定的位置
Hpf.SaveAs(FilePath+"\\"+Path.GetFileName(Hpf.FileName));
}
}
//删除所有的FileUpload控件
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
}
#endregion
}
}
以上就是简单的实现操作,主要注意的地方就是使用HtmlTable控件要添加的命名空间是using System.Web.UI.HtmlControls;和Web.config中配置文件最大上传值:<httpRuntime maxRequestLength="102400"/>其它的代码都有详细的说明,需要的同学可以到我的资源里面去下载参考一下.........