如何仿163的多文件上传带增加删除功能?
最近看了孟子的多文件上传,又想起了163网易网盘上的那个多文件上传功能,于是,改造开始了……
1.ProductUpFile.aspx



















































后台CS页面
usingSystem;
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.IO;
namespaceLabWeb.adminstra
{
///<summary>
///ProductPicUpFile的摘要说明。
///</summary>
publicclassProductPicUpFile:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonUploadButton;
protectedSystem.Web.UI.WebControls.LabelstrStatus;
privatevoidPage_Load(objectsender,System.EventArgse)
{
if(this.IsPostBack)
{
this.SaveImages();
}
//在此处放置用户代码以初始化页面
}
privateBooleanSaveImages()
{
//取得产品Id
stringProductID=Request.QueryString["ProductID"].ToString();
///'遍历File表单元素
HttpFileCollectionfiles=HttpContext.Current.Request.Files;
///'状态信息
System.Text.StringBuilderstrMsg=newSystem.Text.StringBuilder();
strMsg.Append("上传的文件信息分别如下:<hrcolor=red>");
try
{
for(intiFile=0;iFile<files.Count;iFile++)
{
///'检查文件扩展名字
HttpPostedFilepostedFile=files[iFile];
stringfileName,fileExtension;
fileName=System.IO.Path.GetFileName(postedFile.FileName);
if(fileName!="")
{
//重新命名文件名
Randomrd=newRandom();//产生随机数
intvalationNo=10+rd.Next(99);//产生随机数
stringsuiji=valationNo.ToString();//产生随机数
fileName=ProductID+"_"+System.DateTime.Now.ToString("MMddhhmmss")+suiji+fileName;
fileExtension=System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:"+postedFile.ContentType.ToString()+"<br>");
strMsg.Append("客户端文件地址:"+postedFile.FileName+"<br>");
strMsg.Append("上传文件的文件名:"+fileName+"<br>");
strMsg.Append("上传文件的扩展名:"+fileExtension+"<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
//月份
stringYY=System.DateTime.Now.Year.ToString();
stringMM=System.DateTime.Now.Month.ToString();
stringUserDirectory=ProductID;//所要创建文件夹的名字,实际运用中可为用户注册ID
stringUserPath=Server.MapPath("../ProductPicMore").ToString()+"/"+UserDirectory+"/"+YY+"/"+MM;
if(!Directory.Exists(UserPath))//如果文件夹不存在则创建
{
Directory.CreateDirectory(UserPath);
}
stringnewPath="../ProductPicMore/"+UserDirectory+"/"+YY+"/"+MM+"/";//得到服务端图片的虚拟路径
stringSavePath="ProductPicMore/"+UserDirectory+"/"+YY+"/"+MM+"/"+fileName;
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(newPath)+fileName);
SqlConnectioncon=DB.CreateCon();
con.Open();
SqlCommandcmd=newSqlCommand("insertintoProductPic(ProductID,ProductPicName)values('"+ProductID+"','"+SavePath+"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
}
strStatus.Text=strMsg.ToString();
returntrue;
}
catch(System.ExceptionEx)
{
strStatus.Text=Ex.Message;
returnfalse;
}
}
#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}
usingSystem.Collections;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Web;
usingSystem.Web.SessionState;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.IO;
namespaceLabWeb.adminstra
{
///<summary>
///ProductPicUpFile的摘要说明。
///</summary>
publicclassProductPicUpFile:System.Web.UI.Page
{
protectedSystem.Web.UI.WebControls.ButtonUploadButton;
protectedSystem.Web.UI.WebControls.LabelstrStatus;
privatevoidPage_Load(objectsender,System.EventArgse)
{
if(this.IsPostBack)
{
this.SaveImages();
}
//在此处放置用户代码以初始化页面
}
privateBooleanSaveImages()
{
//取得产品Id
stringProductID=Request.QueryString["ProductID"].ToString();
///'遍历File表单元素
HttpFileCollectionfiles=HttpContext.Current.Request.Files;
///'状态信息
System.Text.StringBuilderstrMsg=newSystem.Text.StringBuilder();
strMsg.Append("上传的文件信息分别如下:<hrcolor=red>");
try
{
for(intiFile=0;iFile<files.Count;iFile++)
{
///'检查文件扩展名字
HttpPostedFilepostedFile=files[iFile];
stringfileName,fileExtension;
fileName=System.IO.Path.GetFileName(postedFile.FileName);
if(fileName!="")
{
//重新命名文件名
Randomrd=newRandom();//产生随机数
intvalationNo=10+rd.Next(99);//产生随机数
stringsuiji=valationNo.ToString();//产生随机数
fileName=ProductID+"_"+System.DateTime.Now.ToString("MMddhhmmss")+suiji+fileName;
fileExtension=System.IO.Path.GetExtension(fileName);
strMsg.Append("上传的文件类型:"+postedFile.ContentType.ToString()+"<br>");
strMsg.Append("客户端文件地址:"+postedFile.FileName+"<br>");
strMsg.Append("上传文件的文件名:"+fileName+"<br>");
strMsg.Append("上传文件的扩展名:"+fileExtension+"<br><hr>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
//月份
stringYY=System.DateTime.Now.Year.ToString();
stringMM=System.DateTime.Now.Month.ToString();
stringUserDirectory=ProductID;//所要创建文件夹的名字,实际运用中可为用户注册ID
stringUserPath=Server.MapPath("../ProductPicMore").ToString()+"/"+UserDirectory+"/"+YY+"/"+MM;
if(!Directory.Exists(UserPath))//如果文件夹不存在则创建
{
Directory.CreateDirectory(UserPath);
}
stringnewPath="../ProductPicMore/"+UserDirectory+"/"+YY+"/"+MM+"/";//得到服务端图片的虚拟路径
stringSavePath="ProductPicMore/"+UserDirectory+"/"+YY+"/"+MM+"/"+fileName;
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(newPath)+fileName);
SqlConnectioncon=DB.CreateCon();
con.Open();
SqlCommandcmd=newSqlCommand("insertintoProductPic(ProductID,ProductPicName)values('"+ProductID+"','"+SavePath+"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
}
strStatus.Text=strMsg.ToString();
returntrue;
}
catch(System.ExceptionEx)
{
strStatus.Text=Ex.Message;
returnfalse;
}
}
#regionWeb窗体设计器生成的代码
overrideprotectedvoidOnInit(EventArgse)
{
//
//CODEGEN:该调用是ASP.NETWeb窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
///<summary>
///设计器支持所需的方法-不要使用代码编辑器修改
///此方法的内容。
///</summary>
privatevoidInitializeComponent()
{
this.Load+=newSystem.EventHandler(this.Page_Load);
}
#endregion
}
}
本文地址:http://www.45fan.com/dnjc/73761.html