在开放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;
}

Web文件(图片)上传方法的更多相关文章

  1. web前端图片上传(3)--filereader

    这篇文章主要是为了介绍一种文件上传的方式.当然文件中是包含图片的.如果大家仔细看我的第一篇web前端图片上传(1)就会知道,其实也是按照这种方式上传你的,但是由于上次时间比较紧张,没有详细的介绍今天的 ...

  2. Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程

    Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...

  3. WebUploader文件图片上传插件的使用

    最近在项目中用到了百度的文件图片上传插件WebUploader.分享给大家 需要在http://fex.baidu.com/webuploader/download.html点击打开链接下载WebUp ...

  4. Retrofit 2.0 轻松实现多文件/图片上传/Json字符串/表单

    如果嫌麻烦直接可以用我封装好的库:Novate: https://github.com/Tamicer/Novate 通过对Retrofit2.0的前两篇的基础入门和案例实践,掌握了怎么样使用Retr ...

  5. Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题

    细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法!   在使用Asp.Net Web Api 图片上传接口的时候,到网上找了一些个例子,但大多数找 ...

  6. 细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法!

    在使用Asp.Net Web Api 图片上传接口的时候,到网上找了一些个例子,但大多数找到都是这个版本! [HttpPost] public Task<Hashtable> ImgUpl ...

  7. 富文本vue-quill-editor修改图片上传方法

    富文本vue-quill-editor修改图片上传方法 HTML 代码 HTML codes <!-- 上传的组件 --> <upload style="display:n ...

  8. web前端图片上传

    图片上传有很多种形式,但是听说ios只能传字符串,所以为了安卓.ios和web能用一个接口上传图片,采用了基于base64 的方法上传图片. 下面是我的html <div class=" ...

  9. web文件夹上传

    需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...

  10. java web开发 图片上传功能

    基本思路在于,配置路径,然后用java I/O的api将图片上传到该目录下. String photoPath =    ServletActionContext.getServletContext( ...

随机推荐

  1. C语言探索之旅】 第一部分第四课第三章:变量的世界之显示变量内容

    内容简介 1.课程大纲 2.第一部分第四课第三章:变量的世界之显示变量内容 3.第一部分第五课预告:基本运算 课程大纲 我们的课程分为四大部分,每一个部分结束后都会有练习题,并会公布答案.还会带大家用 ...

  2. 【原创】构建高性能ASP.NET站点之三 细节决定成败

    原文:[原创]构建高性能ASP.NET站点之三 细节决定成败 构建高性能ASP.NET站点之三 细节决定成败 前言:曾经就因为一个小小的疏忽,从而导致了服务器崩溃了,后来才发现:原来就是因为一个循环而 ...

  3. java提高篇(十四)-----关键字final

    在程序设计中,我们有时可能希望某些数据是不能够改变的,这个时候final就有用武之地了.final是java的关键字,它所表示的是"这部分是无法修改的".不想被改变的原因有两个:效 ...

  4. oracle 打开trace,并分析trace

    SQL> oradebug event 10046 trace name context forever,level 8 ORA-00072: process "Unix proces ...

  5. windows下VC界面 DIY系列1----写给想要写界面的C++程序猿的话

    非常早就想写关于C++ UI开发的一系列博文,博客专栏刚审核通过,就立即開始刷博文,不能辜负自己的一番热血,我并非写界面的高手,仅仅想通过写博文提高我自己的技术积累,也顺便帮助大家解决界面开发的瓶颈. ...

  6. 第十九章——使用资源调控器管理资源(2)——使用T-SQL配置资源调控器

    原文:第十九章--使用资源调控器管理资源(2)--使用T-SQL配置资源调控器 前言: 在前一章已经演示了如何使用SSMS来配置资源调控器.但是作为DBA,总有需要写脚本的时候,因为它可以重用及扩展. ...

  7. 系列二VS项目软件配置工具介绍

    原文:系列二VS项目软件配置工具介绍 Svn和VisualSvn介绍 在使用TortoiseSvn(SVN客户端)+ AnkhSvn(VS2008插件) +VisualSvn Server(版本控制服 ...

  8. ADN中国队参加微软Kinect他赢得了全国比赛三等奖,我们的创意项目与团队Kinect于Naviswork虚拟之旅

    以下是我的英语写了一个简短的总结,直接贴出来. 让我们知道我们在这参加Hackathon That's an exciting Hackathon for me and also China team ...

  9. [原] Jenkins Android 自动打包配置(转)

    一.Jenkins自动打包配置 目标:1. 自动打包:2. 自动上传:3. 友好下载 1. Jenkins简介 Jenkins是基于Java开发的一种持续集成工具,用于监控持续重复的工作. 减少重复劳 ...

  10. spring mvc中实现csrf安全防御简记

    1.csrf是什么 csrf全称是Cross-site request forgery,http://en.wikipedia.org/wiki/Csrf 危害:使受害用户在不经意间执行了不是用户意愿 ...