using System;
using System.IO;
using System.Configuration;
using System.Web;
namespace AspNetTest.Common
{
/**//// <summary>
/// 单/多文件上传,,写的不好,见笑。.
/// </summary>
public class UploadFile
{
public UploadFile()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//上传文件默认目录,例如: UploadFiles,该值在web.config中配置
private string initDirectory = ConfigurationSettings.AppSettings["Upload_InitDir"];
//是否启用决定路径,该路径一般会存在数据库中。若设为绝对路径,调用不用考虑相对路径问题
private bool useAbsolutePath = true;
//上传路径,例如:上传路径是 Pic,那么所有的图片都会存在 UploadFiles/Pic目录下,为空时全部存在 UploadFiles目录下
private string uploadPath = "";
//是否启用随机文件名,随机文件名的格式是:yyyyMMddhhmmss + 3位数的随机数
private bool useRandFileName;
//是否自动创建日期目录,若启用,会在上传文件目录下生成 2006/4/17 这3个目录
private bool createDateDir;
public bool UseAbsolutePath
{
get
{
return useAbsolutePath;
}
set
{
useAbsolutePath = value;
}
}
public string UploadPath
{
get
{
return uploadPath;
}
set
{
if(UseAbsolutePath)
{
uploadPath = GetApplicationPath();
}
uploadPath += this.initDirectory + "/";
string valueExtended = value.ToString().Trim();
if(valueExtended != "")
{
uploadPath += value;
if(!valueExtended.EndsWith("/"))
{
uploadPath += "/";
}
}
}
}
public bool UseRandFileName
{
get
{
return useRandFileName;
}
set
{
useRandFileName = value;
}
}
public bool CreateDateDir
{
get
{
return createDateDir;
}
set
{
createDateDir = value;
}
}
public void Upload()
{
HttpFileCollection hpc = HttpContext.Current.Request.Files;
if(hpc != null)
{
for(int i=0; i<hpc.Count; i++)
{
HttpPostedFile file = hpc[i];
//判断大小
int fileContentLength = file.ContentLength;
if(fileContentLength < 1)
{
continue;;
}
int allowMaxSize = Convert.ToInt32(ConfigurationSettings.AppSettings["Upload_AllowMaxSize"]);
if(fileContentLength > allowMaxSize * 1024 * 1024)
{
HttpContext.Current.Response.Write("最大上传的文件只允许<b>" + Convert.ToString(allowMaxSize / 1000) + "M</b>");
return;
}
string fileName = Path.GetFileName(file.FileName);
string fileContentType = Path.GetExtension(fileName).Remove(0, 1);
string allowTypes = ConfigurationSettings.AppSettings["Upload_AllowTypes"];
//判断类型
if(("|"+allowTypes.ToLower()+"|").IndexOf("|"+fileContentType+"|") == -1)
{
HttpContext.Current.Response.Write("只允许上传的文件格式有:<b>" + allowTypes + "</b>");
return;
}
//随机文件名
if(UseRandFileName)
{
fileName = DateTime.Now.ToString("yyyyMMddhhmmss") + (new Random()).Next(100, 1000).ToString() + "." + fileContentType;
}
string addPath = HttpContext.Current.Server.MapPath(UploadPath);
if(!Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
//创建日期目录
if(CreateDateDir)
{
addPath += DateTime.Today.Year.ToString() + @"/";
if(!Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
addPath += DateTime.Today.Month.ToString().PadLeft(2, '0') + @"/";
if(!Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
addPath += DateTime.Today.Day.ToString().PadLeft(2, '0') + @"/";
if(!Directory.Exists(addPath))
{
Directory.CreateDirectory(addPath);
}
}
string filePath = addPath + fileName;
file.SaveAs((filePath));
HttpContext.Current.Response.Write("<br>文件(<b>"+ fileName +"</b>)上传成功!<br>地址:");
HttpContext.Current.Response.Write(filePath + "<hr>");
WriteFilePathToDB(filePath);
}
}
}
public void WriteFilePathToDB(string path)
{
//将上传文件路径写入数据库
}
private string GetApplicationPath()
{
string appPath = HttpContext.Current.Request.ApplicationPath;
if(!appPath.EndsWith("/"))
{
appPath += "/";
}
return appPath;
}
}
}
调用:
UploadFile oUploadFile = new UploadFile();
oUploadFile.UseAbsolutePath = true;
oUploadFile.UploadPath = "Pic";
oUploadFile.CreateDateDir = true;
oUploadFile.UseRandFileName = true;
oUploadFile.Upload();