http://www.cnblogs.com/zcttxs/archive/2012/06/14/2549939.html
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="批量上传文件.aspx.cs" Inherits="批量上传文件" %>
<!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 id="Head1" runat="server">
<title>批量上传文件</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:300px margin:50px auto">
<asp:Label ID="LblMessage" runat="server" Width="300px" ForeColor="#FF0033" Font-Bold="True" Font-Size="Small" />
<table border="1" bordercolor="gray" style="border-collapse: collapse">
<tr>
<td style="text-align: center font-size:10pt font-weight:bold color:DimGray">
批量上传文件
</td>
</tr>
<tr>
<td>
<asp:Panel ID="Pan_UpFile" runat="server" Height="200px" ScrollBars="Auto" Width="250px">
<table id="Tab_UpDownFile" runat="server" cellpadding="0" cellspacing="0" enableviewstate="true">
<tr>
<td style="width: 100px height: 30px">
<asp:FileUpload ID="FileUpload1" runat="server"/>
</td>
</tr>
</table>
</asp:Panel>
</td>
</tr>
<tr>
<td>
<asp:Button ID="BtnAdd" runat="server" Text="添加文件" OnClick="BtnAdd_Click" BorderColor="Gray" BorderWidth="1px" />
<asp:Button ID="BtnUpFile" runat="server" OnClick="BtnUpFile_Click" Text="上传文件" BorderColor="Gray" BorderWidth="1px" />
</td>
</tr>
</table>
</div>
</form>
</body>
</html>
后台批量上传文件.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
public partial class 批量上传文件 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SFUPC()
}
}
#region 该方法是将页面中的上传文件的控件保存到session中
private void SFUPC()
{
//声明一个ArrayList用于存放上传文件的控件
ArrayList AL = new ArrayList()
foreach (Control C in this.Tab_UpDownFile.Controls)
{
if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell HTC = (HtmlTableCell) C.Controls[0]
foreach (Control FUC in HTC.Controls)
{
if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
FileUpload FU = (FileUpload)FUC
FU.BorderColor = System.Drawing.Color.DimGray
FU.BorderWidth = 1
AL.Add(FU)
}
}
}
}
Session.Add("FilesControls", AL)
}
#endregion
#region 该方法用于添加一个上传文件的控件
private void InsertC()
{
//实例化一个ArrayList
ArrayList AL = new ArrayList()
//清除表里的所有行
this.Tab_UpDownFile.Rows.Clear()
//获得Session里存放的上传文件的控件
GetInfo()
//实例化表格的行
HtmlTableRow HTR = new HtmlTableRow()
//实例化表格的列
HtmlTableCell HTC = new HtmlTableCell()
//向列里添加上传控件
HTC.Controls.Add(new FileUpload())
HTR.Controls.Add(HTC)
this.Tab_UpDownFile.Rows.Add(HTR)
SFUPC()
}
#endregion
#region 该方法将session中的上传控件集添加的表格中
private void GetInfo()
{
ArrayList AL = new ArrayList()
if (Session["FilesControls"] != null)
{
AL=(ArrayList)Session["FilesControls"]
foreach (FileUpload FU in AL)
{
HtmlTableRow HTR = new HtmlTableRow()
HtmlTableCell HTC = new HtmlTableCell()
HTC.Controls.Add(FU)
HTR.Controls.Add(HTC)
this.Tab_UpDownFile.Rows.Add(HTR)
}
}
}
#endregion
#region 该方法用于执行文件上传 *** 作
private void UpFile()
{
//获取文件夹路径
string filepath = Server.MapPath("./") + "UpFile"
//获取客服端上载文件的集合
HttpFileCollection HFC = Request.Files
for (int i = 0 i < HFC.Count i++)
{
HttpPostedFile UserHPF =(HttpPostedFile) HFC[i]
try
{
if (UserHPF.ContentLength > 0)
{
UserHPF.SaveAs(filepath+"\\"+System.IO.Path.GetFileName(UserHPF.FileName))
}
}catch
{
this.LblMessage.Text="上传失败!"
}
}
if(Session["FilesControls"]!=null)
{
Session.Remove("FILEsCOntrols")
}
this.LblMessage.Text="上传成功!"
}
#endregion
protected void BtnAdd_Click(object sender, EventArgs e)
{
InsertC()
this.LblMessage.Text = ""
}
protected void BtnUpFile_Click(object sender, EventArgs e)
{
if (this.FileUpload1.PostedFile.FileName != null)
{
UpFile()
SFUPC()
}
else
{
this.LblMessage.Text = "对不起!上传文件不能为空"
}
}
}
批量输入信息 用FOR 循环设置表单(理论上是无限个) 在后台处理数据里面 也用FOR循环 names=request.form("names")(i) ----(i)必须加 <%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/htmlcharset=gb2312"><title>无标题文档</title></head><style type="text/css">BODY { CURSOR: url('p.ani')} </style><body ><% nums=request("nums") if nums=empty then nums=1 else if nums>20 then nums=20 end if end if %><div align="center"><form name="form2" method="post" action="">如果想同时输入N个数据,请设置数据的数目(最多20条):<br><input name="nums" type="text" id="nums2" size="10"><input type="submit" name="Submit" value="提交"></form></div><% for i=1 to nums %><table width="500" align="center" cellpadding="0" cellspacing="0"><form name="form1" method="post" action=4-1.asp?nums=<%=nums%>><tr><td><p align="center"> </p><p align="center">数据 <%=i%><br>姓名: <input name="names" type="text" id="names"><br>密码: <input name="pw" type="text" id="pw"><%next%></p><p align="center"><input type="submit" name="Submit2" value="提交"></p></td></tr></form></table></body></html>这个是表单页,你把代码生成页面看看 <% nums=request.QueryString("nums") for i=1 to nums names=request.Form("names")(i) if names=empty then response.Redirect("4.asp") end if response.End() pw=request.Form("pw")(i) sql="insert into stu1 (names,pw) values ('"&names&"','"&pw&"')" conn.execute (sql) next conn.close response.Write("数据成功录入,谢谢使用") response.Write "<a href=4.asp>返回继续</a>" %>这个是后台录入数据页 根据你自己的需要改动一下欢迎分享,转载请注明来源:内存溢出
评论列表(0条)