ASP.NET多文件上传
By:Roy.LiuLast updated:2008-12-09
///关键在于动态向页面,本例中是TABLE中添加 fileupload 控件。
关键词:htmltablerow,htmltablecell
另外 : HttpFileCollection HFC = Request.Files; request.files 的作用,获得所有要上传的文件,然后再循环处理了。
///
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//首次执行页面
{
SFUPC();//页面执行一次将上传文件控件集到缓存中
}
}
protected void Button1_Click(object sender, EventArgs e)
{
UpFile();//执行上传文件
}
protected void Button2_Click(object sender, EventArgs e)
{
InsertC();//执行添加控件方法
}
private void SFUPC()//该方法用于保存当前页面上传文件控件集到缓存中
{
ArrayList AL = new ArrayList();//创建动态增加数组
foreach (Control C in F.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;
AL.Add(FU);
}
}
}
}
Session.Add("FilesControls", AL);
}
private void InsertC()//该方法用于添加一个上传文件的控件
{
// ArrayList AL = new ArrayList();
this.F.Rows.Clear();
GetInfo();
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add(new FileUpload());
HTR.Controls.Add(HTC);
F.Rows.Add(HTR);
SFUPC();
}
private void GetInfo()//该方法用于读取缓存中存储的上传文件控件集
{
ArrayList AL = new ArrayList();
if (Session["FilesControls"] != null)
{
AL = (System.Collections.ArrayList)Session["FilesControls"];
for (int i = 0; i < AL.Count; i++)
{
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]);
HTR.Controls.Add(HTC);
F.Rows.Add(HTR);
}
}
}
private void UpFile()//该方法用于执行文件上传操作
{
if (FileUpload1.PostedFile.FileName == "")
{
Response.Write("");
return;
}
else
{
string FilePath = Server.MapPath("./") + "Files";
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile UserHPF = HFC[i];
try
{
if (UserHPF.ContentLength > 0)
{
UserHPF.SaveAs(FilePath + "\\" + System.IO.Path.GetFileName(UserHPF.FileName));
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=123456;Database=mrdb");
con.Open();
string str = "select count(*) from tb_files where name='" + System.IO.Path.GetFileName(UserHPF.FileName) + "'";
SqlCommand mycom = new SqlCommand(str, con);
int intcont = Convert.ToInt32(mycom.ExecuteScalar());
if (intcont > 0)
{
Response.Write("");
return;
}
else
{
SqlCommand com = new SqlCommand("Insert INTO tb_files(name,fileload)values('" + System.IO.Path.GetFileName(UserHPF.FileName) + "','" + FilePath + "')", con);
com.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
Response.Write("");
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("InFo.aspx");
}
}
关键词:htmltablerow,htmltablecell
另外 : HttpFileCollection HFC = Request.Files; request.files 的作用,获得所有要上传的文件,然后再循环处理了。
///
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)//首次执行页面
{
SFUPC();//页面执行一次将上传文件控件集到缓存中
}
}
protected void Button1_Click(object sender, EventArgs e)
{
UpFile();//执行上传文件
}
protected void Button2_Click(object sender, EventArgs e)
{
InsertC();//执行添加控件方法
}
private void SFUPC()//该方法用于保存当前页面上传文件控件集到缓存中
{
ArrayList AL = new ArrayList();//创建动态增加数组
foreach (Control C in F.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;
AL.Add(FU);
}
}
}
}
Session.Add("FilesControls", AL);
}
private void InsertC()//该方法用于添加一个上传文件的控件
{
// ArrayList AL = new ArrayList();
this.F.Rows.Clear();
GetInfo();
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add(new FileUpload());
HTR.Controls.Add(HTC);
F.Rows.Add(HTR);
SFUPC();
}
private void GetInfo()//该方法用于读取缓存中存储的上传文件控件集
{
ArrayList AL = new ArrayList();
if (Session["FilesControls"] != null)
{
AL = (System.Collections.ArrayList)Session["FilesControls"];
for (int i = 0; i < AL.Count; i++)
{
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]);
HTR.Controls.Add(HTC);
F.Rows.Add(HTR);
}
}
}
private void UpFile()//该方法用于执行文件上传操作
{
if (FileUpload1.PostedFile.FileName == "")
{
Response.Write("");
return;
}
else
{
string FilePath = Server.MapPath("./") + "Files";
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile UserHPF = HFC[i];
try
{
if (UserHPF.ContentLength > 0)
{
UserHPF.SaveAs(FilePath + "\\" + System.IO.Path.GetFileName(UserHPF.FileName));
SqlConnection con = new SqlConnection("server=(local);user id=sa;pwd=123456;Database=mrdb");
con.Open();
string str = "select count(*) from tb_files where name='" + System.IO.Path.GetFileName(UserHPF.FileName) + "'";
SqlCommand mycom = new SqlCommand(str, con);
int intcont = Convert.ToInt32(mycom.ExecuteScalar());
if (intcont > 0)
{
Response.Write("");
return;
}
else
{
SqlCommand com = new SqlCommand("Insert INTO tb_files(name,fileload)values('" + System.IO.Path.GetFileName(UserHPF.FileName) + "','" + FilePath + "')", con);
com.ExecuteNonQuery();
}
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
}
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
Response.Write("");
}
protected void Button3_Click(object sender, EventArgs e)
{
Response.Redirect("InFo.aspx");
}
}
From:一号门
Previous:pagecontrol动态创建tabsheet并选择关闭右键菜单等
Next:自定义水晶报表(先收藏,以后可能用到)
COMMENTS