在开放Web应用程序的时候经常会遇到图片或者是文件上传的模块,这里就是该模块的实现的后台方法
上传图片方法
/// <summary>
/// 功能:上传图片方法
/// </summary>
/// <param name="moduleType">运营模块枚举类型</param>
/// <param name="toFileApplicationPath">mappath指定的要保存的路径</param>
/// <param name="toShowApplictionFilePath">客户端显示相对路径</param>
/// <param name="hpf">文件域对象</param>
/// <returns>返回处理结果(这里处理结果是以json形式表示)</returns>
public static void UploadImage(ModuleType moduleType, HttpPostedFile hpf, string toFileApplicationPath, string toShowApplictionFilePath)
{
//json字符串
string jsonStr = string.Empty;
//生成要保存的文件名
string fileName = string.Empty;
//完整路径
string toFile = string.Empty;
try
{
if (hpf.ContentLength < )
{
if (!string.IsNullOrEmpty(hpf.FileName))
{
//获取文件扩展名
string fileNameExt = Path.GetExtension(hpf.FileName).ToLower().Trim();
if (CheckImageExt(fileNameExt))
{
switch (moduleType)
{
//产品图片
case ModuleType.product:
fileName = CreateFileName(ModuleType.product);
break;
//logo图片
case ModuleType.logo:
fileName = CreateFileName(ModuleType.logo);
break;
//幻灯片
case ModuleType.slides:
fileName = CreateFileName(ModuleType.slides);
break;
case ModuleType.newsIndexImg:
fileName = CreateFileName(ModuleType.newsIndexImg);
break;
case ModuleType.customer:
fileName = CreateFileName(ModuleType.customer);
break;
case ModuleType.productCategory:
fileName = CreateFileName(ModuleType.productCategory);
break;
case ModuleType.productCategoryIndex:
fileName = CreateFileName(ModuleType.productCategoryIndex);
break;
case ModuleType.server:
fileName = CreateFileName(ModuleType.server);
break;
case ModuleType.memberHeadImg:
fileName = CreateFileName(ModuleType.memberHeadImg);
break;
case ModuleType.caseInfo:
fileName = CreateFileName(ModuleType.caseInfo);
break;
case ModuleType.caseMultiImg:
fileName = CreateFileName(ModuleType.caseMultiImg);
break;
case ModuleType.payment:
fileName = CreateFileName(ModuleType.payment);
break;
case ModuleType.player:
fileName = CreateFileName(ModuleType.player);
break;
case ModuleType.productMultiImg:
fileName = CreateFileName(ModuleType.productMultiImg);
break;
case ModuleType.teacherImg:
fileName = CreateFileName(ModuleType.teacherImg);
break;
default:
break;
}
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(toFileApplicationPath);
//检查是否有该路径,没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//获得要保存的相对路径
string applicationFilePath = toShowApplictionFilePath + fileName + fileNameExt;
//得大始图像对象(如果用户上传的图片比较需求规定的最小规格还小则不给于缩略图处理)
toFile = toFileFullPath + fileName + fileNameExt;
hpf.SaveAs(toFile);
HttpContext.Current.Response.ContentType = "text/HTML";
string showClient = toShowApplictionFilePath + fileName + fileNameExt;
jsonStr = "{\"msg\":\"success\",\"imgPath\":\"" + showClient + "\",\"golobImgDesc\":\"" + "" + "\",\"dataImgPath\":\"" + applicationFilePath + "\",\"showClient\":\"" + showClient + "\"}";
HttpContext.Current.Response.Write(jsonStr);
}
else
{
HttpContext.Current.Response.ContentType = "text/HTML";
jsonStr = "{\"msg\":\"errorExtens\"}";
HttpContext.Current.Response.Write(jsonStr);
}
}
else
{
HttpContext.Current.Response.ContentType = "text/HTML";
jsonStr = "{\"msg\":\"notUploadFile\"}";
HttpContext.Current.Response.Write(jsonStr);
}
}
else
{
HttpContext.Current.Response.ContentType = "text/HTML";
jsonStr = "{\"msg\":\"imgBig2M\"}";
HttpContext.Current.Response.Write(jsonStr);
}
}
catch (Exception)
{
throw;
}
}
上传文件方法
/// <summary>
/// 功能:上传文件方法
/// </summary>
/// <param name="moduleType">运营模块枚举类型</param>
/// <param name="toFileApplicationPath">mappath指定的要保存的路径</param>
/// <param name="toShowApplictionFilePath">客户端显示相对路径</param>
/// <param name="hpf">文件域对象</param>
/// <returns>返回处理结果(这里处理结果是以json形式表示)</returns>
public static void UploadFile(ModuleType moduleType, HttpPostedFile hpf, string toFileApplicationPath, string toShowApplictionFilePath)
{
//json字符串
string jsonStr = string.Empty;
//生成要保存的文件名
string fileName = string.Empty;
//完整路径
string toFile = string.Empty;
try
{
if (!string.IsNullOrEmpty(hpf.FileName))
{
//获取文件扩展名
string fileNameExt = Path.GetExtension(hpf.FileName).ToLower().Trim();
if (CheckFileExt(fileNameExt))
{
switch (moduleType)
{
case ModuleType.productFile:
fileName = CreateFileName(ModuleType.productFile);
break;
case ModuleType.adFile:
fileName = CreateFileName(ModuleType.adFile);
break;
case ModuleType.downloadFile:
fileName = CreateFileName(ModuleType.downloadFile);
break;
default:
break;
}
//物理完整路径
string toFileFullPath = HttpContext.Current.Server.MapPath(toFileApplicationPath);
//检查是否有该路径,没有就创建
if (!Directory.Exists(toFileFullPath))
{
Directory.CreateDirectory(toFileFullPath);
}
//获得要保存的相对路径
string applicationFilePath = toShowApplictionFilePath + fileName + fileNameExt;
toFile = toFileFullPath + fileName + fileNameExt;
hpf.SaveAs(toFile);
HttpContext.Current.Response.ContentType = "text/HTML";
string showClient = toShowApplictionFilePath + fileName + fileNameExt;
jsonStr = "{\"msg\":\"success\",\"imgPath\":\"" + showClient + "\",\"golobImgDesc\":\"" + "" + "\",\"filePath\":\"" + applicationFilePath + "\",\"showClient\":\"" + showClient + "\"}";
HttpContext.Current.Response.Write(jsonStr);
}
else
{
HttpContext.Current.Response.ContentType = "text/HTML";
jsonStr = "{\"msg\":\"errorExtens\"}";
HttpContext.Current.Response.Write(jsonStr);
}
}
else
{
HttpContext.Current.Response.ContentType = "text/HTML";
jsonStr = "{\"msg\":\"notUploadFile\"}";
HttpContext.Current.Response.Write(jsonStr);
}
}
catch (Exception)
{
throw;
}
}
是否为合法的图片
/// <summary>
/// 功能:检查是否为合法的上传文件
/// </summary>
/// <param name="fileExt"></param>
/// <returns></returns>
public static bool CheckImageExt(string fileExt)
{
string[] allowExt = new string[] { ".gif", ".jpg", ".jpeg", ".png" };
for (int i = 0; i < allowExt.Length; i++)
{
if (allowExt[i].ToLower().Trim() == fileExt.ToLower().Trim()) { return true; }
}
return false;
}
检查是否为合法的上传文件
/// <summary>
/// 功能:检查是否为合法的上传文件
/// </summary>
/// <param name="fileExt"></param>
/// <returns></returns>
public static bool CheckFileExt(string fileExt)
{
string[] allowExt = new string[] { ".pdf", ".doc", ".zip",".rar" };
for (int i = 0; i < allowExt.Length; i++)
{
if (allowExt[i].ToLower().Trim() == fileExt.ToLower().Trim()) { return true; }
}
return false;
}