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 文件的上传与管理
{
public partial class FileUp : System.Web.UI.Page
{
CommonClass cc = new CommonClass();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SaveFUC();
}
}
protected void SaveFUC()//保存当前页面上传文件控件集到缓存中
{
ArrayList AL = new ArrayList();//创建动态数组
foreach (Control C in tabFU.Controls)
{
if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell HTC = (HtmlTableCell)C.Controls[0];
foreach (Control FUC in HTC.Controls)
{
//判断是否为上传控件(Fileuplode)如果是添加到ArrayList中
if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
FileUpload FU = (FileUpload)FUC;
AL.Add(FU);
}
}
}
}
//将Arraylist中所有的上传控件添加到缓存中
Session.Add("FilesControl", AL);
}
public void GetFUCInfo()
{
ArrayList AL = new ArrayList();
if (Session["FilesControl"] != null)
{
//将缓存中上传控件集放到array中
AL = (System.Collections.ArrayList)Session["FilesControl"];
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);
tabFU.Rows.Add(HTR);//将上传控件添加到名为tabFU的表格中
}
}
}
protected void InsertFUC()
{
ArrayList AL = new ArrayList();
this.tabFU.Rows.Clear();
//调用GetFUCInfo方法,将放在缓存中的控件添加到表格中
GetFUCInfo();
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add(new FileUpload());
HTR.Controls.Add(HTC);
tabFU.Rows.Add(HTR);
SaveFUC();
}
public static int IntlsUF = 0;
protected void UPFile()//执行上传操作,并插入数据表中
{
string serverPath = Server.MapPath(".//") + "Files";//保存到服务器的路径
HttpFileCollection HFC = Request.Files;
for (int i = 0; i < HFC.Count; i++)
{
HttpPostedFile UseHPF = HFC[i];//对客户上传的文件单独访问
try
{
if (UseHPF.ContentLength > 0)
{
//调用GetAuto方法获得文件自动编号
int IntFieldID = cc.GetAutoID("fileID", "信息表");
//文件真名
//实现多个文件上传,不被覆盖
string strFileTName = "[" + IntFieldID + "]" + System.IO.Path.GetFileName(UseHPF.FileName);
//定义字符串,将上传文件保存到数据库中
string sqlStr = "insert into 信息表(fileID,fileName,fileUpDate,fileLoad,fileTrueName)";
sqlStr += " values('" + IntFieldID + "'";
sqlStr += ",'" + System.IO.Path.GetFileName(UseHPF.FileName) + "'";
sqlStr += ",'" + DateTime.Now.ToLongDateString() + "'";
sqlStr += ",'" + serverPath + "'";
sqlStr += ",'" + strFileTName + "')";
cc.Execu(sqlStr);
//将上传的文件放到指定的文件夹中
UseHPF.SaveAs(serverPath+"//"+strFileTName);
IntlsUF = 1;
}
}
catch
{
//上传失败时,清空缓存,从新上传
if (Session["FilesControl"] != null)
{
Session.Remove("FilesControl");
}
Response.Write(cc.MessageBox("处理出错", "FileUp.aspx"));
return;
}
//当上传成功或者没有上传文件时,清空上传控件集
if (Session["FilesControl"] != null)
{
Session.Remove("FilesControl");
}
if (IntlsUF == 1)
{
Response.Write(cc.MessageBox("处理成功", "FileUp.aspx"));
}
else
{
Response.Write(cc.MessageBox("请选择上传文件", "FileUp.aspx"));
}
}
}
protected void bt_addUplode_Click(object sender, EventArgs e)
{
InsertFUC();
}
protected void bt_uplode_Click(object sender, EventArgs e)
{
UPFile();
}
}
}
问题已解决 虚拟路径的问题