/// <summary>
/// 提供上传文件时所使用的方法
/// </summary>
publicinterfaceIUploadFile
{
/// <summary>
/// 验证上传文件的扩展名
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <returns></returns>
boolCheckFileType(System.Web.HttpPostedFile postFile);
/// <summary>
/// 获取文件名称
/// 调用此方法并不创建新目录
/// </summary>
/// <param name="filePath">服务器的虚拟路径</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
stringGetFileName(stringfilePath,stringfileName);
}
/// <summary>
/// 提供上传文件时所使用的方法
/// </summary>
publicabstractclassUploadFile
{
/// <summary>
/// 获取文件路径
/// </summary>
/// <param name="path">虚拟路径</param>
/// <returns></returns>
protectedstringGetDirectory(stringpath)
{
stringdirectoryName = DateTime.Now.ToString("yyyyMMdd");
stringfilePath = Path.Combine(path, directoryName);
if(!Directory.Exists(filePath))
{
returnPath.Combine(filePath, GetNumber(1));
}
returnPath.Combine(filePath, GetPath(Directory.GetDirectories(filePath), filePath));
}
/// <summary>
/// 获取文件夹中的文个数件少于1000的文件夹名称
/// </summary>
/// <param name="info">目录名称</param>
/// <param name="filePath">路径</param>
/// <returns></returns>
protectedstringGetPath(IList<string> info, stringfilePath)
{
stringfileName = string.Empty;
if(info == null)
{
returnnull;
}
if(info.Count <= 0)
{
returnGetNumber(1);
}
for(inti = 0; i < info.Count; i++)
{
if(Directory.Exists(info[i]))
{
if(Directory.GetFiles(info[i]).Length < 1000)
{
fileName = info[i];
break;
}
}
}
if(string.IsNullOrEmpty(fileName))
{
returnPath.Combine(filePath, GetNumber(int.Parse(info.ToList<string>().Max(s => s.Substring(s.LastIndexOf("\\") + 1).TrimStart('0'))) + 1));
}
else
{
returnfileName;
}
}
/// <summary>
/// 获取数字字符串,不足4位用0补足
/// </summary>
/// <param name="number">不大于9999的数字</param>
/// <returns></returns>
protectedstringGetNumber(intnumber)
{
if(number > 9999) returnnull;
return(10000 + number).ToString().Substring(1);
}
#region IUploadFile 成员
/// <summary>
/// 验证上传文件的扩展名
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <returns></returns>
publicabstractboolCheckFileType(System.Web.HttpPostedFile postFile);
/// <summary>
/// 获取文件名称
/// </summary>
/// <param name="filePath">服务器的虚拟路径</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
publicvirtualstringGetFileName(stringfilePath,stringfileName)
{
DateTime now = DateTime.Now;
stringname = (100 + now.Hour).ToString().Substring(1) + (100 + now.Minute).ToString().Substring(1) + (100 + now.Second).ToString().Substring(1) + "_"+ fileName;
returnPath.Combine(GetDirectory(filePath), name);
}
#endregion
}
publicclassLBCUploadFile:UploadFile,IUploadFile
{
/// <summary>
/// 验证上传文件的扩展名
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <returns></returns>
publicoverrideboolCheckFileType(System.Web.HttpPostedFile postFile)
{
IList<string> fileType = newList<string>();
fileType.Add("image/gif");
fileType.Add("image/jpeg");
fileType.Add("image/png");
returnfileType.Contains(postFile.ContentType);
}
}
/// <summary>
/// 文件上传管理类
/// </summary>
publicclassUploadFileManager
{
privatestringfileName=null;
privatestringerrInfo;
privateIUploadFile uploadFile;
privateintfileLength=1024*1024;
/// <summary>
/// 初始化类实例
/// </summary>
publicUploadFileManager()
{
uploadFile = newLBCUploadFile();
}
/// <summary>
/// 初始化类实例
/// </summary>
/// <param name="uploadFile">实现了IUploadFile接口的对象</param>
publicUploadFileManager(IUploadFile uploadFile)
{
this.uploadFile = uploadFile;
}
/// <summary>
/// 获取或设置文件名,如果上传失败则为null
/// </summary>
publicstringFileName
{
get
{
returnfileName;
}
}
/// <summary>
/// 获取或设置上传文件的大小,单位为字节,默认为1024*1024
/// </summary>
publicintFileLength
{
get
{
returnfileLength;
}
set
{
fileLength = value;
}
}
/// <summary>
/// 获取文件上传失败的信息
/// </summary>
publicstringErrorInfo
{
get
{
returnerrInfo;
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <param name="filePath">服务器的虚拟路径</param>
publicboolUploadFile(System.Web.HttpPostedFile postFile, stringfilePath)
{
if(postFile == null)
{