在开放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. 【Linux探索之旅】第一部分第二课:下载Linux,免费的噢

    内容简介 1.第一部分第二课:下载Linux,免费的噢 2.第一部分第三课预告:测试并安装Ubuntu 下载Linux,免费的噢 大家好,上一课我们认识了非常“霸气侧漏”的Linux操作系统. 也知道 ...

  2. C#如何获得 WINDOWS 版本号

    using System; using System.Runtime.InteropServices; namespace GetWindowsVersion { [ StructLayout( La ...

  3. vb.net它SqlHelper制备及应用

    上次文章中说到.对于一个项目来说.SqlHelper是一个非常重要的类. 在正在构造的机房收费系统中.有大量的操作数据库的操作. 现在.把反复的代码所有拿出来,就形成了SqlHelper类.这个Sql ...

  4. java.io.NotSerializableException

    结果发现序列不成功非静态内部类时的序列中,出现以下异常: java.io.NotSerializableException: com.tang.sharedpreferencesdemo.MainAc ...

  5. 于linux已安装moodle

    本文介绍了两个虚拟机的安装linux server 及相关服务,随着后这些基础.安装应用程序服务 moodle 2.7+  它是使用最广泛的平台,网络课程. 在安装过程中moodle之前,需要支持软件 ...

  6. unity3d 学习笔记(两)

    AudioClip 使用声音资源 unity3d资源可以被设置为声3d声音或2d声音.3d间的影响,越近声音越大 component: Audio source: 声音的发生物体 Audio list ...

  7. 吐槽一下Activiti用户手册和一本书

    业余没事的时候,读点Java轮廓,无意中发现Activiti.我们打算跑了几个例子来看看它是如何. 我们一直从事低层次.我们在上面的照顾偶尔有精确地的程度不是什么. 这个过程是如此悲惨开始.第一Act ...

  8. 高效C++规划

    推荐写C++代码风格.看似easy.坚持不易,且写且珍惜! --陈国林 1. 版本号和版本号声明 版本号和版本号文件声明位于头文件和定义文件的开头,主要内容 (1)版本号信息 (2)文件名.标识符.摘 ...

  9. mvc验证jquery.unobtrusive-ajax

    Unobtrusive Ajax Ajax (Asynchronous JavaScript and XML 的缩写),如我们所见,这个概念的重点已经不再是XML部分,而是 Asynchronous ...

  10. [Python]新手写爬虫全过程(转)

    今天早上起来,第一件事情就是理一理今天该做的事情,瞬间get到任务,写一个只用python字符串内建函数的爬虫,定义为v1.0,开发中的版本号定义为v0.x.数据存放?这个是一个练手的玩具,就写在tx ...