在开放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. 你的第一个AngularJS应用--教程二:基架、建立和測试的工具

    介绍 有非常多可用的工具能够帮助你开发AngularJS 应用,那些非常复杂的框架不在我的讨论范围之中,这也是我開始这系列教程的原因. 在第一部分,我们掌握了AngularJS框架的基本结构,开发了第 ...

  2. 流动python - 自然装饰

    好多人搞非常复杂的装饰,其实本质easy. 首先,这是什么装饰?发现穿着在代码@xxx帽子,它是装饰. 它是由如何定制它装饰? 其实不管什么人需要一个参数callable用来做装饰器,比方函数和类.为 ...

  3. 两次fclose引发的血案

    代码本来在Windows上开发的,功能基本完毕迁移到Linux上,结果一跑,乱象重重.这里只列出两个.   一崩溃: /mnt/diskc/db/app/bin/mysqld: double free ...

  4. 类似的微博推断客户关系sql声明

    类别似新浪微博的关注和共同关心 不知道别人是怎么设计的. 反正我是例如以下设计的 ID   USER   FRIEND 1     A         B 2      B         A 3   ...

  5. Java操作memcached(一)

    Memcached事实上,两次Hash算法    第一次hash算法被用于定位Memcached示例    第二次hash算法是底部HashMap中间hash算法 Hash算法      1.依据余数 ...

  6. Bag标签成一条线的代码来实现中国字

    说明: <Bag id=书包名 act=2words[name=key] [gap=字符] [quotes=引號]>中英文混合内容</Bag> 例0: 默认分词(无gap和qu ...

  7. tomcatserver解析(六)-- Acceptor

    Acceptor负责用来管理连接到tomcatserver的数量,来看看Acceptor在tomcatserver中的应用,是怎样实现连接管理的,socket连接建立成功之后,是怎样实现内容的读写的( ...

  8. [2011山东ACM省赛] Identifiers(模拟)

    Identifiers Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^ 题目描写叙述  Identifier is an important ...

  9. 几个cd快速提示

    cd是project师每天都会用到的命令. 今天就来分享几条和cd有关的小技巧 cd 假设你用cd ~来进入当前用户的home文件夹的话,那么能够试试直接敲cd. 相同效果,少敲两下键盘. cd - ...

  10. Dev GridView RowCellClick活动MouseDown事件

    GridView可编辑.在无声的思想左键点击"进入编辑". 将GridView的OptionsColumn.AllowEdit至false离开时触发RowCellClick. 但有 ...