/// <summary>
/// 提供上传文件时所使用的方法
/// </summary>
public interface IUploadFile
{
/// <summary>
/// 验证上传文件的扩展名
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <returns></returns>
bool CheckFileType(System.Web.HttpPostedFile postFile);
/// <summary>
/// 获取文件名称
/// 调用此方法并不创建新目录
/// </summary>
/// <param name="filePath">服务器的虚拟路径</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
string GetFileName(string filePath,string fileName);
}
/// <summary>
/// 提供上传文件时所使用的方法
/// </summary>
public abstract class UploadFile
{
/// <summary>
/// 获取文件路径
/// </summary>
/// <param name="path">虚拟路径</param>
/// <returns></returns>
protected string GetDirectory(string path)
{
string directoryName = DateTime.Now.ToString("yyyyMMdd");
string filePath = Path.Combine(path, directoryName);
if (!Directory.Exists(filePath))
{
return Path.Combine(filePath, GetNumber(1));
}
return Path.Combine(filePath, GetPath(Directory.GetDirectories(filePath), filePath));
}
/// <summary>
/// 获取文件夹中的文个数件少于1000的文件夹名称
/// </summary>
/// <param name="info">目录名称</param>
/// <param name="filePath">路径</param>
/// <returns></returns>
protected string GetPath(IList<string> info, string filePath)
{
string fileName = string.Empty;
if (info == null)
{
return null;
}
if (info.Count <= 0)
{
return GetNumber(1);
}
for (int i = 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))
{
return Path.Combine(filePath, GetNumber(int.Parse(info.ToList<string>().Max(s => s.Substring(s.LastIndexOf("//") + 1).TrimStart('0'))) + 1));
}
else
{
return fileName;
}
}
/// <summary>
/// 获取数字字符串,不足4位用0补足
/// </summary>
/// <param name="number">不大于9999的数字</param>
/// <returns></returns>
protected string GetNumber(int number)
{
if (number > 9999) return null;
return (10000 + number).ToString().Substring(1);
}
#region IUploadFile 成员
/// <summary>
/// 验证上传文件的扩展名
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <returns></returns>
public abstract bool CheckFileType(System.Web.HttpPostedFile postFile);
/// <summary>
/// 获取文件名称
/// </summary>
/// <param name="filePath">服务器的虚拟路径</param>
/// <param name="fileName">文件名</param>
/// <returns></returns>
public virtual string GetFileName(string filePath,string fileName)
{
DateTime now = DateTime.Now;
string name = (100 + now.Hour).ToString().Substring(1) + (100 + now.Minute).ToString().Substring(1) + (100 + now.Second).ToString().Substring(1) + "_" + fileName;
return Path.Combine(GetDirectory(filePath), name);
}
#endregion
}
public class LBCUploadFile:UploadFile,IUploadFile
{
/// <summary>
/// 验证上传文件的扩展名
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <returns></returns>
public override bool CheckFileType(System.Web.HttpPostedFile postFile)
{
IList<string> fileType = new List<string>();
fileType.Add("image/gif");
fileType.Add("image/jpeg");
fileType.Add("image/png");
return fileType.Contains(postFile.ContentType);
}
}
/// <summary>
/// 文件上传管理类
/// </summary>
public class UploadFileManager
{
private string fileName=null;
private string errInfo;
private IUploadFile uploadFile;
private int fileLength=1024*1024;
/// <summary>
/// 初始化类实例
/// </summary>
public UploadFileManager()
{
uploadFile = new LBCUploadFile();
}
/// <summary>
/// 初始化类实例
/// </summary>
/// <param name="uploadFile">实现了IUploadFile接口的对象</param>
public UploadFileManager(IUploadFile uploadFile)
{
this.uploadFile = uploadFile;
}
/// <summary>
/// 获取或设置文件名,如果上传失败则为null
/// </summary>
public string FileName
{
get
{
return fileName;
}
}
/// <summary>
/// 获取或设置上传文件的大小,单位为字节,默认为1024*1024
/// </summary>
public int FileLength
{
get
{
return fileLength;
}
set
{
fileLength = value;
}
}
/// <summary>
/// 获取文件上传失败的信息
/// </summary>
public string ErrorInfo
{
get
{
return errInfo;
}
}
/// <summary>
/// 上传文件
/// </summary>
/// <param name="postFile">System.Web.HttpPostedFile对象</param>
/// <param name="filePath">服务器的虚拟路径</param>
public bool UploadFile(System.Web.HttpPostedFile postFile, string filePath)
{
if (postFile == null)
{
errInfo = "参数postFile为null!";
return false;
}
if (postFile.ContentLength <= 0)
{
errInfo = "没有要上传的文件!";
return false;
}
if (postFile.ContentLength > fileLength)
{
errInfo = "上传的文件太大!";
return false;
}
if (!uploadFile.CheckFileType(postFile))
{
errInfo = "上传文件格式不正确!";
return false;
}
try
{
string uploadFileName = uploadFile.GetFileName(filePath,Path.GetFileName(postFile.FileName));
string path = Path.GetDirectoryName(uploadFileName);
if (!Directory.Exists(path))
{
try
{
if (Directory.CreateDirectory(path) == null)
{
errInfo = "创建目录失败!";
return false;
}
}
catch
{
errInfo = "创建目录失败!";
return false;
}
}
int count = 0;
while (true)
{
if (File.Exists(uploadFileName))
{
count++;
string fileName = Path.GetFileName(uploadFileName);
uploadFileName =Path.Combine(path,(1000000 + count).ToString().Substring(1) + "_" + fileName);
}
else
{
try
{
postFile.SaveAs(uploadFileName);
fileName = uploadFileName;
return true;
}
catch (System.Web.HttpException e)
{
errInfo = e.Message;
return false;
}
}
}
}
catch (System.Web.HttpException e)
{
errInfo = "上传文件时出现异常,异常消息为:"+e.Message;
return false;
}
}
}