自定义一个upload.ascx用户自定义控件
C#代码为:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Collections.Generic;
public partial class upload : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
FileUploadAdd();
}
/// <summary>
/// 控制上传对象的属性如width,height,size上传的文件大小,filecount上传的总数量
/// </summary>
private static string width = "500px", height = "300px", size = "0", type = "", filecount = "0", path = "";
/// <summary>
/// 设置上传后返回的文件名称
/// </summary>
private static ArrayList FileNameList = new ArrayList();
/// <summary>
/// 获取上传文件后的文件名称
/// </summary>
public ArrayList GetFileNameList { get { return FileNameList; } }
/// <summary>
/// 保存上传文件的本地路径
/// </summary>
protected static ArrayList arr = new ArrayList();
/// <summary>
/// 保存上传文件的HttpPostedFile上传基础对象
/// </summary>
protected static List<HttpPostedFile> list = new List<HttpPostedFile>();
/// <summary>
/// 获取或设置空间的宽度,默认500px
/// </summary>
public string Width { get { return width; } set { width = value; } }
/// <summary>
/// 获取或设置空间的高度,默认300px
/// </summary>
public string Height { get { return height; } set { height = value; } }
/// <summary>
/// 获取或设置限定文件上传大小,默认为0 未限定
/// </summary>
public int FileSize { get { return int.Parse(size); } set { size = value.ToString(); } }
/// <summary>
/// 获取或设置限定上传文件的类型,默认无限定
/// </summary>
public string FileType { get { return type; } set { type = value; } }
/// <summary>
/// 获取或设置上传文件的总数量,默认为0无限定
/// </summary>
public int FileCount { get { return int.Parse(filecount); } set { filecount = value.ToString(); } }
/// <summary>
/// 获取或设置上传文件的路径
/// </summary>
public string FilePath { get { return path; } set { path = value; } }
/// <summary>
/// 判断选择上传的文件大小跟类型
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void bntIfFileUplaod_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string fileName = FileUpload1.PostedFile.FileName;
string typefile = fileName.Split('.')[fileName.Split('.').Length - 1].ToString();
if (FileType != "")
{
string[] ty = FileType.Split('|');
int or = 0;
foreach (string tys in ty)
{
if (tys == typefile || tys.ToUpper() == typefile.ToUpper()) { or++; }
}
if (or == 0) { FileUploadAdd(); alert(bntIfFileUplaod, "请上传限定的文件" + FileType + "!"); return; }
}
if (FileSize != 0)
{
if (FileUpload1.PostedFile.ContentLength / 1024 > FileSize)
{
FileUploadAdd();
alert(bntIfFileUplaod, "文件超出限定大小,当前文件为" + FileUpload1.PostedFile.ContentLength / 1024 + "KB!"); return;
}
}
list.Add(FileUpload1.PostedFile);
AddTextFile(fileName);
}
else
{
FileUploadAdd();
alert(bntIfFileUplaod, "请选择文件!");
}
}
/// <summary>
/// 添加文本,显示路径
/// </summary>
/// <param name="value">完整的路径值</param>
private void AddTextFile(string value)
{
int or = 0;
for (int z = 0; z < arr.Count; z++)
{
if (arr[z].ToString() == value)
{
or++;
}
}
if (or == 0)
{
arr.Add(value);
if (FileCount != 0)
{
if (arr.Count - FileCount != 1)
{
FileUploadAdd();
}
else
{
FileUploadAdd();
alert(bntIfFileUplaod, "最多可上传文件数量 " + FileCount + " 条!");
}
}
else
{
FileUploadAdd();
}
}
else
{
FileUploadAdd();
alert(bntIfFileUplaod, "已有相同的文件存在,请重新选择!");
}
}
/// <summary>
/// 绑定上传文件
/// </summary>
protected void FileUploadAdd()
{
DataTable table = new DataTable("files");
DataColumn c1 = new DataColumn("filepath", typeof(String));
DataColumn c2 = new DataColumn("id", typeof(Int32));
table.Columns.Add(c2);
table.Columns.Add(c1);
for (int i = 1; i <= arr.Count; i++)
{
table.Rows.Add(table.NewRow());
table.Rows[i - 1]["filepath"] = arr[i - 1].ToString();
table.Rows[i - 1]["id"] = i.ToString();
}
ReptAddFile.DataSource = table;
ReptAddFile.DataBind();
}
/// <summary>
/// 对话框文本信息
/// </summary>
/// <param name="UpdatePanel">刷新控件ID</param>
/// <param name="strMsg">要弹出的信息</param>
/// <returns></returns>
private void alert(Control UpdatePanel, string strMsg)
{
ScriptManager.RegisterStartupScript(UpdatePanel, typeof(UpdatePanel), "alert", "alert('" + strMsg + "');", true);
}
/// <summary>
/// 开始上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void bntUploadFile_Click(object sender, EventArgs e)
{
for (int i = 0; i < ReptAddFile.Items.Count; i++)
{
string pathName = ((Label)ReptAddFile.Items[i].FindControl("lblFilePath")).Text;
string[] pathN = Path.GetFileName(pathName).Split('.');
string files = GetDateTimeString() + "." + pathN[1].ToString();
pathName = Server.MapPath(FilePath + files);
HttpPostedFile postFile = list[i];
postFile.SaveAs(pathName);
FileNameList.Add(files);
System.Threading.Thread.Sleep(20);
}
list.Clear();
arr.Clear();
alert(bntUploadFile, "上传成功!");
FileUploadAdd();
}
/// <summary>
/// 删除上传文件
/// </summary>
/// <param name="source"></param>
/// <param name="e"></param>
protected void ReptAddFile_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("delete"))
{
string name = ((Label)e.Item.FindControl("lblFilePath")).Text;
for (int i = 0; i < arr.Count; i++)
{
if (arr[i].ToString() == name)
{
arr.RemoveAt(i);
list.RemoveAt(i);
}
}
FileUploadAdd();
}
}
/// <summary>
/// 获取上传时显示的进度条
/// </summary>
/// <param name="path">限定的文件类型</param>
/// <returns></returns>
protected string GetFileTypePath(string path)
{
string[] sp = path.Split('|');
string name = "";
foreach (string paths in sp)
{
name += "<font style='color:Red; font-size:13px;'>" + paths + "</font> ,";
}
return name.Substring(0, name.LastIndexOf(',')).ToString();
}
/// <summary>
/// 获取一个带毫秒的时间字符串
/// </summary>
/// <returns></returns>
private string GetDateTimeString()
{
string date = string.Empty;
string[] imgpt = DateTime.Now.ToString().Split(' ');
string[] imgpt1 = imgpt[0].ToString().Split('-');
string[] imgpt2 = imgpt[1].ToString().Split(':');
string mill = DateTime.Now.Millisecond.ToString();
date = imgpt1[0] + imgpt1[1] + imgpt1[2] + imgpt2[0] + imgpt2[1] + imgpt2[2] + mill;
return date;
}
}