作者:小山
using System;
using System.Web.UI.HtmlControls;
using System.IO;
![](/Images/OutliningIndicators/None.gif)
namespace Document.Bll
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/ContractedBlock.gif)
{
public class HtmlInputFileControl
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
HtmlInputFileControlHtmlInputFileControl#region HtmlInputFileControl
public HtmlInputFileControl()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IsAllowedExtension是否允许该扩展名上传IsAllowedExtension是否允许该扩展名上传#region IsAllowedExtension是否允许该扩展名上传
public static bool IsAllowedExtension(HtmlInputFile hifile)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strOldFilePath = "",strExtension = "";
![](/Images/OutliningIndicators/InBlock.gif)
//允许上传的扩展名,可以改成从配置文件中读出
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
string[] arrExtension =
{".gif",".jpg",".jpeg",".bmp",".png"};
![](/Images/OutliningIndicators/InBlock.gif)
if(hifile.PostedFile.FileName != string.Empty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//判断该扩展名是否合法
for(int i = 0; i< arrExtension.Length; i++)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(strExtension.Equals(arrExtension[i]))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return true;
}
}
}
return false;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
IsAllowedLength判断上传文件大小是否超过最大值IsAllowedLength判断上传文件大小是否超过最大值#region IsAllowedLength判断上传文件大小是否超过最大值
public static bool IsAllowedLength(HtmlInputFile hifile)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//允许上传文件大小的最大值,可以保存在xml文件中,单位为KB
int i = 20;
//如果上传文件的大小超过最大值,返回flase,否则返回true.
if(hifile.PostedFile.ContentLength > i * 1024)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return false;
}
return true;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
SaveFile上传文件并返回文件名SaveFile上传文件并返回文件名#region SaveFile上传文件并返回文件名
public static string SaveFile(HtmlInputFile hifile,string strAbsolutePath)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
string strOldFilePath = "",strExtension = "",strNewFileName = "";
![](/Images/OutliningIndicators/InBlock.gif)
if(hifile.PostedFile.FileName != string.Empty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
strOldFilePath = hifile.PostedFile.FileName;
//取得上传文件的扩展名
strExtension = strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));
//文件上传后的命名
strNewFileName = GetUniqueString() + strExtension;
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hifile.PostedFile.SaveAs(strAbsolutePath + strNewFileName);
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
hifile.PostedFile.SaveAs(strAbsolutePath + "\\" + strNewFileName);
}
}
return strNewFileName;
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
CoverFile重新上传文件,删除原有文件CoverFile重新上传文件,删除原有文件#region CoverFile重新上传文件,删除原有文件
public static void CoverFile(HtmlInputFile ffFile,string strAbsolutePath,string strOldFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//获得新文件名
string strNewFileName = GetUniqueString();
![](/Images/OutliningIndicators/InBlock.gif)
if(ffFile.PostedFile.FileName != string.Empty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//旧图片不为空时先删除旧图片
if(strOldFileName != string.Empty)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
DeleteFile(strAbsolutePath,strOldFileName);
}
SaveFile(ffFile,strAbsolutePath);
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
DeleteFile删除指定文件DeleteFile删除指定文件#region DeleteFile删除指定文件
public static void DeleteFile(string strAbsolutePath, string strFileName)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(strAbsolutePath.LastIndexOf("\\") == strAbsolutePath.Length)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(File.Exists(strAbsolutePath + strFileName))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(strAbsolutePath + strFileName);
}
}
else
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if(File.Exists(strAbsolutePath + "\\" + strFileName))
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
File.Delete(strAbsolutePath + "\\" + strFileName);
}
}
}
#endregion
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ContractedSubBlock.gif)
GetUniqueString获取一个不重复的文件名GetUniqueString获取一个不重复的文件名#region GetUniqueString获取一个不重复的文件名
public static string GetUniqueString()
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return DateTime.Now.ToString("yyyyMMddhhmmss");
}
#endregion
}
}