效果圖
![](https://www.cnblogs.com/images/cnblogs_com/xishi/86266/o_簡單文件上傳控件.JPG)
還有一個Js文件FielUpLoad.js
var i = 1;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
function AddFileToList(obj)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
//添加一行
var clientID=obj.parentElement.parentElement.parentElement.parentElement.id;
var newTr = document.getElementById(clientID).insertRow();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//添加两列
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var newTd0 = newTr.insertCell();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var newTd1 = newTr.insertCell();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var newTd2 = newTr.insertCell();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//设置列内容和属性
var file = document.createElement("input");
file.type = "file";
file.id = file.name = "file" + i;
file.name = "title";
newTd0.appendChild(file);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
var btn = document.createElement("input");
btn.type = "button";
btn.id = btn.name = "btn" + i;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.value = "移除";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btn.onclick = function()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
var index=window.event.srcElement.parentElement.parentElement.rowIndex;
document.getElementById(clientID).deleteRow(index);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
newTd1.appendChild(btn);
i++;
return false;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
設計文件FileUpLoad.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
namespace FielUpLoad
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
{
[ToolboxData("<{0}:FielUpLoad runat=server></{0}:FielUpLoad>")]
public class FielUpLoad : WebControl, INamingContainer
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
屬性定義#region 屬性定義
private Button btnAddFileToList;//添加文件按鈕
private Button btnStartUpload;//文件上傳按鈕
private HtmlInputFile hifFile;//文件瀏覽控件
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Bindable(true)]
[Category("Appearance")]
[DefaultValue(1048576)]
[Localizable(true)]
[Description("文件總共最大的長度(單位KB)預設是1024*1024KB")]
public int TotalMaxFileLength
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
ViewState["TotalMaxFileLength"] = value;
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
return ((ViewState["TotalMaxFileLength"] == null) ? 1048576 : (int)ViewState["TotalMaxFileLength"]);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Bindable(true)]
[Category("Appearance")]
[DefaultValue(1024)]
[Localizable(true)]
[Description("單個文件最大的長度(單位KB)預設是1024KB")]
public int SingleMaxFileLength
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
ViewState.Add("SingleMaxFileLength", value);
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
return ((ViewState["SingleMaxFileLength"] == null) ? 1024 : (int)ViewState["SingleMaxFileLength"]);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Category("Appearance"), Description("文件要存放的相對路徑如~\\存放在根目錄")]
public string ServerFilePath
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
ViewState.Add("ServerFilePath", value);
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
String s = (String)ViewState["ServerFilePath"];
return ((s == null) ? "~\\" : s);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
[Bindable(true)]
[Category("Appearance")]
[DefaultValue(true)]
[Localizable(true)]
[Description("上傳文件名是否保留,還是重新產生")]
public bool FileNameHold
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
set
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
ViewState.Add("FileNameHold",value);
}
get
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();
return ((ViewState["FileNameHold"] == null) ? true : (bool)ViewState["FileNameHold"]);
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
顯示消息的函數#region 顯示消息的函數
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 顯示消息的函數
/// </summary>
/// <param name="java"></param>
protected void ShowMessage(string sMessage)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "fsf", "<script language=javascript>alert('" + sMessage + "');</script>");
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
創建子控件#region 創建子控件
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 創建子控件
/// </summary>
protected override void CreateChildControls()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Controls.Clear();
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.ServerFilePath = @"~\";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
hifFile = new HtmlInputFile();
hifFile.ID = "HtmlInputFilehifFile";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btnAddFileToList = new Button();
btnAddFileToList.Text = "添加";
btnAddFileToList.ID = "btnAddFileToList";
btnAddFileToList.OnClientClick = "return AddFileToList(this)";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
btnStartUpload = new Button();
btnStartUpload.Text = "上傳";
btnStartUpload.ID = "btnStartUpload";
btnStartUpload.Click += new EventHandler(btnStartUpload_Click);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
this.Controls.Add(hifFile);
this.Controls.Add(btnAddFileToList);
this.Controls.Add(btnStartUpload);
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
控件布局#region 控件布局
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 控件布局
/// </summary>
/// <param name="writer"></param>
protected override void Render(HtmlTextWriter writer)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
EnsureChildControls();//這句好重要啊,我開始沒有這句,控件拖出來就有錯誤
AddAttributesToRender(writer);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
hifFile.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
btnAddFileToList.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
btnStartUpload.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderEndTag(); // Tr
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
writer.RenderEndTag(); // Table
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
重寫OnPreRender#region 重寫OnPreRender
protected override void OnPreRender(EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (this.Page != null)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
this.Page.ClientScript.RegisterClientScriptResource(this.GetType(), "FielUpLoad.FielUpLoad.js");
}
base.OnPreRender(e);
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
單擊按鈕上傳文件#region 單擊按鈕上傳文件
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 單擊按鈕上傳文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnStartUpload_Click(object sender, System.EventArgs e)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
SaveAs();
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
文件上傳另存為#region 文件上傳另存為
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 文件上傳另存為
/// </summary>
/// <returns>文件存放路徑以及文件名如:C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite2\ada\+20070302044811.jpg,20070302044811.jpg,</returns>
public string SaveAs()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
string sFileSavePath = GetAbsolutePath(@ServerFilePath);
string sReturnFileName =sFileSavePath+"+";
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//判斷文件大小是否超出
if (!IsAllowedLength())
return "";
for (int i = 0; i < files.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (files[i].FileName.Length > 0)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string sFileSavePathName = "";
if (FileNameHold)
sFileSavePathName = sFileSavePath + System.IO.Path.GetFileName(files[i].FileName);
else
sFileSavePathName = sFileSavePath + GetUniqueString()+i.ToString()+ System.IO.Path.GetExtension(files[i].FileName);
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
files[i].SaveAs(sFileSavePathName);
sReturnFileName =sReturnFileName+ System.IO.Path.GetFileName(sFileSavePathName)+",";
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
return sReturnFileName;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
根據相對路徑取得絕對路徑#region 根據相對路徑取得絕對路徑
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 根據相對路徑取得絕對路徑
/// </summary>
/// <param name="sFilePath">相對路徑</param>
/// <returns></returns>
private string GetAbsolutePath(string sFilePath)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
try
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//取得絕對路徑
string sAbsolutePath = HttpContext.Current.Request.MapPath(sFilePath);
//判断路径是否存在,若不存在则创建路径
DirectoryInfo upDir = new DirectoryInfo(sAbsolutePath);
if (!upDir.Exists)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
upDir.Create();
}
//如果路径末尾为\符号,则直接返回路徑
if (sAbsolutePath.LastIndexOf("\\") != sAbsolutePath.Length)
sAbsolutePath += "\\";
return sAbsolutePath;
}
catch
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
throw new Exception("由相對路徑取得絕對路徑失敗!");
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
获取一个不重复的文件名#region 获取一个不重复的文件名
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 获取一个不重复的文件名
/// </summary>
/// <returns>得到的文件名形如:20050922101010</returns>
private string GetUniqueString()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif)
判断上传文件大小是否合乎要求#region 判断上传文件大小是否合乎要求
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//// <summary>
/// 判断上传文件大小是否合乎要求
/// </summary>
/// <returns></returns>
public bool IsAllowedLength()
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bool bAllowed = true;
HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
double dTotalLength = 0;
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
//判斷上传文件的大小超过單個最大值
for (int i = 0; i < files.Count; i++)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (files[i].ContentLength / 1024.0 > SingleMaxFileLength)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bAllowed =false;
ShowMessage("上傳文件失敗,單個文件超過允許最大值!");
break;
}
dTotalLength += files[i].ContentLength / 1024.0;
}
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
if (dTotalLength > TotalMaxFileLength)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
bAllowed = false;
ShowMessage("上傳文件失敗,縂文件超過允許最大值!");
}
return bAllowed;
}
#endregion
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
}
}
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)