using System;
using System.Web.UI.HtmlControls;
using System.IO;
public class HtmlInputFileControl
{
#region IsAllowedExtension是否允许该扩展名上传
///// <summary>
///// 是否允许该扩展名上传
///// </summary>
///// <param name="hifile">控件名</param>
///// <returns>bool</returns>
public static bool IsAllowedExtension(HtmlInputFile hifile)
{
string strOldFilePath = "", strExtension = "";
//允许上传的扩展名,可以改成从配置文件中读出
string[] arrExtension = { ".gif", ".jpg", ".jpeg", ".bmp", ".png" };
if (hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//判断该扩展名是否合法
for (int i = 0; i < arrExtension.Length; i++)
{
if (strExtension.Equals(arrExtension[i]))
{
return true;
}
}
}
return false;
}
#endregion
#region IsAllowedLength判断上传文件大小是否超过最大值
/**//// <summary>
/// 判断上传文件大小是否超过最大值
/// </summary>
/// <param name="hifile">控件</param>
/// <returns>bool</returns>
public static bool IsAllowedLength(HtmlInputFile hifile)
{
//允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
int i = int.Parse(System.Configuration.ConfigurationManager.AppSettings["maxRequestLength"].ToString());//取得允许图片大小
//如果上传文件的大小超过最大值,返回flase,否则返回true.
if(hifile.PostedFile.ContentLength > i * 1024)
{
return false;
}
return true;
}
#endregion
#region SaveFile上传文件并返回文件名
/**//// <summary>
/// 上传文件并返回文件名
/// </summary>
/// <param name="hifile">控件</param>
/// <param name="strAbsolutePath">文件夹名称</param>
/// <returns></returns>
public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
{
string strOldFilePath = "",strExtension = "",strNewFileName = "";
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//文件上传后的命名
strNewFileName = GetUniqueString() + strExtension;
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
{
hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
}
else
{
hifile.PostedFile.SaveAs(strAbsolutePath + "\\" + strNewFileName);
}
}
return strNewFileName;
}
#endregion
#region CoverFile重新上传文件,删除原有文件
/**//// <summary>
/// 重新上传文件,删除原有文件
/// </summary>
/// <param name="ffFile">控件</param>
/// <param name="strAbsolutePath">文件夹名称</param>
/// <param name="strOldFileName">旧文件名</param>
public static string CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
{
string strNewFileName = "";
if(ffFile.PostedFile.FileName != string.Empty)
{
//旧图片不为空时先删除旧图片
if(strOldFileName != string.Empty)
{
DeleteFile(strAbsolutePath,strOldFileName);
}
strNewFileName = SaveFile(ffFile,strAbsolutePath);
}
return strNewFileName;
}
#endregion
#region DeleteFile删除指定文件
/**//// <summary>
/// 删除指定文件
/// </summary>
/// <param name="strAbsolutePath">文件夹名称</param>
/// <param name="strFileName">文件名</param>
public static void DeleteFile(string strAbsolutePath, string strFileName)
{
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
{
if(File.Exists(strAbsolutePath + strFileName))
{
File.Delete(strAbsolutePath + strFileName);
}
}
else
{
if(File.Exists(strAbsolutePath + "\\" + strFileName))
{
File.Delete(strAbsolutePath + "\\" + strFileName);
}
}
}
#endregion
#region GetUniqueString获取一个不重复的文件名
/**//// <summary>
/// 获取一个不重复的文件名
/// </summary>
/// <returns></returns>
public static string GetUniqueString()
{
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion
}
注意:当遇到文件太大的时候需要在web.config里面的system.web节点下加入
<httpRuntime maxRequestLength="102400" executionTimeout="360"/>
using System.Web.UI.HtmlControls;
using System.IO;
public class HtmlInputFileControl
{
#region IsAllowedExtension是否允许该扩展名上传
///// <summary>
///// 是否允许该扩展名上传
///// </summary>
///// <param name="hifile">控件名</param>
///// <returns>bool</returns>
public static bool IsAllowedExtension(HtmlInputFile hifile)
{
string strOldFilePath = "", strExtension = "";
//允许上传的扩展名,可以改成从配置文件中读出
string[] arrExtension = { ".gif", ".jpg", ".jpeg", ".bmp", ".png" };
if (hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//判断该扩展名是否合法
for (int i = 0; i < arrExtension.Length; i++)
{
if (strExtension.Equals(arrExtension[i]))
{
return true;
}
}
}
return false;
}
#endregion
#region IsAllowedLength判断上传文件大小是否超过最大值
/**//// <summary>
/// 判断上传文件大小是否超过最大值
/// </summary>
/// <param name="hifile">控件</param>
/// <returns>bool</returns>
public static bool IsAllowedLength(HtmlInputFile hifile)
{
//允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
int i = int.Parse(System.Configuration.ConfigurationManager.AppSettings["maxRequestLength"].ToString());//取得允许图片大小
//如果上传文件的大小超过最大值,返回flase,否则返回true.
if(hifile.PostedFile.ContentLength > i * 1024)
{
return false;
}
return true;
}
#endregion
#region SaveFile上传文件并返回文件名
/**//// <summary>
/// 上传文件并返回文件名
/// </summary>
/// <param name="hifile">控件</param>
/// <param name="strAbsolutePath">文件夹名称</param>
/// <returns></returns>
public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
{
string strOldFilePath = "",strExtension = "",strNewFileName = "";
if(hifile.PostedFile.FileName != string.Empty)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//文件上传后的命名
strNewFileName = GetUniqueString() + strExtension;
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
{
hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
}
else
{
hifile.PostedFile.SaveAs(strAbsolutePath + "\\" + strNewFileName);
}
}
return strNewFileName;
}
#endregion
#region CoverFile重新上传文件,删除原有文件
/**//// <summary>
/// 重新上传文件,删除原有文件
/// </summary>
/// <param name="ffFile">控件</param>
/// <param name="strAbsolutePath">文件夹名称</param>
/// <param name="strOldFileName">旧文件名</param>
public static string CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
{
string strNewFileName = "";
if(ffFile.PostedFile.FileName != string.Empty)
{
//旧图片不为空时先删除旧图片
if(strOldFileName != string.Empty)
{
DeleteFile(strAbsolutePath,strOldFileName);
}
strNewFileName = SaveFile(ffFile,strAbsolutePath);
}
return strNewFileName;
}
#endregion
#region DeleteFile删除指定文件
/**//// <summary>
/// 删除指定文件
/// </summary>
/// <param name="strAbsolutePath">文件夹名称</param>
/// <param name="strFileName">文件名</param>
public static void DeleteFile(string strAbsolutePath, string strFileName)
{
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
{
if(File.Exists(strAbsolutePath + strFileName))
{
File.Delete(strAbsolutePath + strFileName);
}
}
else
{
if(File.Exists(strAbsolutePath + "\\" + strFileName))
{
File.Delete(strAbsolutePath + "\\" + strFileName);
}
}
}
#endregion
#region GetUniqueString获取一个不重复的文件名
/**//// <summary>
/// 获取一个不重复的文件名
/// </summary>
/// <returns></returns>
public static string GetUniqueString()
{
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion
}
注意:当遇到文件太大的时候需要在web.config里面的system.web节点下加入
<httpRuntime maxRequestLength="102400" executionTimeout="360"/>