Web文件(图片)上传方法
在开放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文件(图片)上传方法的更多相关文章
- web前端图片上传(3)--filereader
这篇文章主要是为了介绍一种文件上传的方式.当然文件中是包含图片的.如果大家仔细看我的第一篇web前端图片上传(1)就会知道,其实也是按照这种方式上传你的,但是由于上次时间比较紧张,没有详细的介绍今天的 ...
- Asp.Net Core Web Api图片上传(一)集成MongoDB存储实例教程
Asp.Net Core Web Api图片上传及MongoDB存储实例教程(一) 图片或者文件上传相信大家在开发中应该都会用到吧,有的时候还要对图片生成缩略图.那么如何在Asp.Net Core W ...
- WebUploader文件图片上传插件的使用
最近在项目中用到了百度的文件图片上传插件WebUploader.分享给大家 需要在http://fex.baidu.com/webuploader/download.html点击打开链接下载WebUp ...
- Retrofit 2.0 轻松实现多文件/图片上传/Json字符串/表单
如果嫌麻烦直接可以用我封装好的库:Novate: https://github.com/Tamicer/Novate 通过对Retrofit2.0的前两篇的基础入门和案例实践,掌握了怎么样使用Retr ...
- Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题
细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法! 在使用Asp.Net Web Api 图片上传接口的时候,到网上找了一些个例子,但大多数找 ...
- 细谈 Web Api 图片上传,在使用 Task.ContinueWith 变量无法赋值问题的解决办法!
在使用Asp.Net Web Api 图片上传接口的时候,到网上找了一些个例子,但大多数找到都是这个版本! [HttpPost] public Task<Hashtable> ImgUpl ...
- 富文本vue-quill-editor修改图片上传方法
富文本vue-quill-editor修改图片上传方法 HTML 代码 HTML codes <!-- 上传的组件 --> <upload style="display:n ...
- web前端图片上传
图片上传有很多种形式,但是听说ios只能传字符串,所以为了安卓.ios和web能用一个接口上传图片,采用了基于base64 的方法上传图片. 下面是我的html <div class=" ...
- web文件夹上传
需求:项目要支持大文件上传功能,经过讨论,初步将文件上传大小控制在500M内,因此自己需要在项目中进行文件上传部分的调整和配置,自己将大小都以501M来进行限制. 第一步: 前端修改 由于项目使用的是 ...
- java web开发 图片上传功能
基本思路在于,配置路径,然后用java I/O的api将图片上传到该目录下. String photoPath = ServletActionContext.getServletContext( ...
随机推荐
- MongoDB时间处理问题
MongoDB保存到数据库的时候,默认为UTC时间,在数据库保存时,会和当前时间有个间隔,差距为8小时. 在读取的时候,需要再次转换回来,比较麻烦. 其实,Mongo本身就已经提供了相应的处理方法,即 ...
- LeetCode :: Convert Sorted Array (link list) to Binary Search Tree [tree]
1.Given an array where elements are sorted in ascending order, convert it to a height balanced BST. ...
- JavaScript 初识Promise 对象
什么是Promise? 其实, Promise就是一个类,而且这个类已经成为ES6的标准,是 ECMAScript 6 规范的重要特性之一.这个类目前在chrome32.Opera19.Firefox ...
- Mina框架与Spring整合配置文件
Mina框架与Spring的整合事实上非常easy,主要是要弄清楚要注入的属性的名称,进而选择合适的注入方法. 关于Spring的四种注入方法请还有一篇文章:spring依赖注入的四种方式 <? ...
- Windows编译Nodejs时遇到 File "configure", line 313 SyntaxError: invalid syntax Failed to create vc project files. 时的解决方法
第一次编译的时候电脑上未安装python,遂下载了python最新版本3.3.3,但是报了下面这个错误. 把python降到2.7.*的版本即可. 我这里测试2.7.6和2.7.3版本可以正常编译.
- SQL Server 性能优化(一)——简介
原文:SQL Server 性能优化(一)--简介 一.性能优化的理由: 听起来有点多余,但是还是详细说一下: 1.节省成本:这里的成本不一定是钱,但是基本上可以变相认为是节省钱.性能上去了,本来要投 ...
- hdu1023
import java.math.BigInteger; import java.util.Scanner; public class Main { static BigInteger fac(Big ...
- linux Apache rotatelogs 故障原因及解决方案未生效
rotatelogs 截断日志.构造.但保存vhost.conf 之后.serverhttpd -k restart 还是无法成功重新启动. 日志文件: (2)No such file or dire ...
- CSS3添加属性选择: [attribute*=value] 、[attribute^=value] 和[attribute$=value]
在CSS3新的 [attribute*=value] .[attribute^=value] 和[attribute$=value] 三个选择.使得属性选择使用通配符概念. 下面是利用这三个属性样本代 ...
- Dp_F Pku1157
<span style="color:#000099;">/* F - 简单dp Time Limit:1000MS Memory Limit:10000KB 64bi ...