最近看了孟子的多文件上传,又想起了163网易网盘上的那个多文件上传功能,于是,改造开始了……
1.ProductUpFile.aspx



















































后台CS页面
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace LabWeb.adminstra
{
/// <summary>
/// ProductPicUpFile 的摘要说明。
/// </summary>
public class ProductPicUpFile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
this.SaveImages();
}
// 在此处放置用户代码以初始化页面
}
private Boolean SaveImages()
{
//取得产品Id
string ProductID=Request.QueryString["ProductID"].ToString();
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件信息分别如下:<hr color=red>");
try
{
for(int iFile = 0; iFile <files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
//重新命名文件名
Random rd = new Random();//产生随机数
int valationNo = 10 + rd.Next(99);//产生随机数
string suiji = 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>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
//月份
string YY=System.DateTime.Now.Year.ToString();
string MM=System.DateTime.Now.Month.ToString();
string UserDirectory = ProductID;//所要创建文件夹的名字,实际运用中可为用户注册ID
string UserPath = Server.MapPath("../ProductPicMore").ToString() + "/" + UserDirectory + "/"+YY+"/" + MM;
if (!Directory.Exists(UserPath)) //如果文件夹不存在则创建
{
Directory.CreateDirectory(UserPath);
}
string newPath = "../ProductPicMore/" + UserDirectory + "/" + YY+"/"+MM+"/";//得到服务端图片的虚拟路径
string SavePath="ProductPicMore/" + UserDirectory + "/" + YY+"/"+MM+"/"+fileName;
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(newPath) + fileName);
SqlConnection con=DB.CreateCon();
con.Open();
SqlCommand cmd =new SqlCommand("insert into ProductPic(ProductID,ProductPicName) values('"+ProductID+"','"+SavePath+"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
namespace LabWeb.adminstra
{
/// <summary>
/// ProductPicUpFile 的摘要说明。
/// </summary>
public class ProductPicUpFile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
if (this.IsPostBack)
{
this.SaveImages();
}
// 在此处放置用户代码以初始化页面
}
private Boolean SaveImages()
{
//取得产品Id
string ProductID=Request.QueryString["ProductID"].ToString();
///'遍历File表单元素
HttpFileCollection files = HttpContext.Current.Request.Files;
/// '状态信息
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上传的文件信息分别如下:<hr color=red>");
try
{
for(int iFile = 0; iFile <files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if (fileName != "")
{
//重新命名文件名
Random rd = new Random();//产生随机数
int valationNo = 10 + rd.Next(99);//产生随机数
string suiji = 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>");
///'可根据扩展名字的不同保存到不同的文件夹
///注意:可能要修改你的文件夹的匿名写入权限。
//月份
string YY=System.DateTime.Now.Year.ToString();
string MM=System.DateTime.Now.Month.ToString();
string UserDirectory = ProductID;//所要创建文件夹的名字,实际运用中可为用户注册ID
string UserPath = Server.MapPath("../ProductPicMore").ToString() + "/" + UserDirectory + "/"+YY+"/" + MM;
if (!Directory.Exists(UserPath)) //如果文件夹不存在则创建
{
Directory.CreateDirectory(UserPath);
}
string newPath = "../ProductPicMore/" + UserDirectory + "/" + YY+"/"+MM+"/";//得到服务端图片的虚拟路径
string SavePath="ProductPicMore/" + UserDirectory + "/" + YY+"/"+MM+"/"+fileName;
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath(newPath) + fileName);
SqlConnection con=DB.CreateCon();
con.Open();
SqlCommand cmd =new SqlCommand("insert into ProductPic(ProductID,ProductPicName) values('"+ProductID+"','"+SavePath+"')",con);
cmd.ExecuteNonQuery();
con.Close();
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}