方法里包括了图片大小限制、图片尺寸、文件内容等等的判断。。。

该案例是mvc下的demo,支持单张图片上传。

public ActionResult Upload()
{
string imgurl = "";
foreach (string key in Request.Files)
{
//这里只测试上传第一张图片file[0]
HttpPostedFileBase file0 = Request.Files[key]; //转换成byte,读取图片MIME类型
Stream stream;
int size = file0.ContentLength / 1024; //文件大小KB if (size > 1024)
{
return Content(ReturnMsg(Enum_Return.失败, "图片不能超过1M:", null));
} byte[] fileByte = new byte[2];//contentLength,这里我们只读取文件长度的前两位用于判断就好了,这样速度比较快,剩下的也用不到。
stream = file0.InputStream;
stream.Read(fileByte, 0, 2);//contentLength,还是取前两位 //获取图片宽和高
//System.Drawing.Image image = System.Drawing.Image.FromStream(stream);
//int width = image.Width;
//int height = image.Height; string fileFlag = "";
if (fileByte != null && fileByte.Length > 0)//图片数据是否为空
{
fileFlag = fileByte[0].ToString() fileByte[1].ToString();
}
string[] fileTypeStr = { "255216", "7173", "6677", "13780" };//对应的图片格式jpg,gif,bmp,png
if (fileTypeStr.Contains(fileFlag))
{
string action = Request["action"];
string path = "/uploads/";
switch (action)
{
case "headimage":
path = "headimage/";
break;
case "blogtype":
path = "blogtype/";
break;
}
string fullpath = path UserInfo.userID "/";
if (!Directory.Exists(Server.MapPath(fullpath)))
{
Directory.CreateDirectory(Server.MapPath(fullpath));
} Request.Files[key].SaveAs(Server.MapPath(fullpath Request.Files[key].FileName));
imgurl = fullpath Request.Files[key].FileName;
}
else
{
return Content(ReturnMsg(Enum_Return.失败, "图片格式不正确:" fileFlag, null));
} stream.Close();
} return Content(ReturnMsg(Enum_Return.成功, "上传成功", imgurl));
}

一般处理程序

public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "application/json";
HttpPostedFile _upfile = context.Request.Files["File"];
if (_upfile.ContentLength < 500000)
{
if (string.IsNullOrEmpty(_upfile.FileName))
{
context.Response.Write("请上传图片");
}
string fileFullname = _upfile.FileName;
string dataName = DateTime.Now.ToString("yyyyMMddhhmmss");
string fileName = fileFullname.Substring(fileFullname.LastIndexOf("\\") 1);
string type = fileFullname.Substring(fileFullname.LastIndexOf(".") 1);
if (type == "bmp" || type == "jpg" || type == "gif" || type == "JPG" || type == "BMP" || type == "GIF")
{
_upfile.SaveAs(HttpContext.Current.Server.MapPath("photo") "\\" dataName "." type);
HttpCookie cookie = new HttpCookie("photo");
context.Response.Write("上传成功");
}
else
{
context.Response.Write("支持格式:|jpg|gif|bmp|");
}
}
else
{
context.Response.Write("你的图片已经超过500K的大小!");
}
}

C# 最齐全的上传图片方法的更多相关文章

  1. C#最齐全的上传图片方法。

    public ActionResult Upload() { string imgurl = ""; foreach (string key in Request.Files) { ...

  2. IOS上传图片方法类

    IOS上传图片方法类   iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...

  3. php form表单ajax上传图片方法

    form表单ajax上传图片方法 先引用jquery.form.js 前台代码<pre><form id="form1"> <input id=&qu ...

  4. ASP.NET - 上传图片方法(单张)

    /// <summary> /// 上传图片 /// </summary> /// <param name="fileupload">上传的控件 ...

  5. 【方法】移动端H5如何调用相册和相机上传图片、音频、视频

    在移动端上传图片方法很简单,使用HTML5中的input:file供文件上传. <一>常用属性值: 1.accept:规定文件上传来提交的文件类型,此属性只能和type:file配合使用 ...

  6. angularjs上传图片

    通过AngularJS实现图片上传及缩略图展示(读取文件内容) AngularJS图片上传功能的实现(读取文件内容) AngularJs实现Multipart/form-data 文件的上传(上传文件 ...

  7. ios中摄像头/相册获取图片,压缩图片,上传服务器方法总结

    相册 iphone的相册包含摄像头胶卷+用户计算机同步的部分照片.用户可以通过UIImagePickerController类提供的交互对话框来从相册中选择图像.但是,注意:相册中的图片机器路径无法直 ...

  8. Thinkphp框架 -- ajax无刷新上传图片

    用Thinkphp框架做无刷新上传图片 视图层 View <!doctype html> <html lang="en"> <head> < ...

  9. Web文件(图片)上传方法

    在开放Web应用程序的时候经常会遇到图片或者是文件上传的模块,这里就是该模块的实现的后台方法 上传图片方法 /// <summary> /// 功能:上传图片方法 /// </sum ...

随机推荐

  1. redis基本知识

    启动redis: redis-sever 检查redis是否在运行: redis-cli 再ping 会提示PONG 启动远程redis:redis-cli -h 127.0.0.1 -p 6379 ...

  2. spring 之 BeanPostProcessor

    粗略一看, 它有这么多实现: 可见, 它是多么基础 而重要的一个 接口啊! 它提供了两个方法: public interface BeanPostProcessor { Object postProc ...

  3. Jenkins部署报weblogic.deploy.api.tools.deployer.DeployerException: Java heap space

    Jenkins部署报weblogic.deploy.api.tools.deployer.DeployerException: Java heap space异常 解决办法: 在MAVEN_OPTS中 ...

  4. PHP生成HTML静态页面。

    function Generate(){ $html = '<!DOCTYPE html><html lang="en"><head> < ...

  5. hitTest,UIWindow sendEvent ,touchbegan, 响应链

    https://developer.apple.com/documentation/uikit/touches_presses_and_gestures/using_responders_and_th ...

  6. Mybaits

    1.Mybatis全注解形式  (在注解上不能直接使用动态Sql,必须要在前后面加上<script>SQL</script>标签,否则会报错): @Select("& ...

  7. SQL Server中有关约束(constraint)的一些细节

    本文出处:http://www.cnblogs.com/wy123/p/7350265.html (保留出处并非什么原创作品权利,本人拙作还远远达不到,仅仅是为了链接到原文,因为后续对可能存在的一些错 ...

  8. 550 5.7.1 Client does not have permissions to send as this sender

    收发邮件时出现以上这种情况,系统提示550 5.7.1 Client does not have permissions to send as this sender,这是什么原因赞成的呢? 活动目录 ...

  9. ubuntu开启远程shell,开启上传下载

    需要先安装openshell-server 具体命令如下: 1.先更新下源 sudo apt-get update 2.安装openshell-server sudo apt-get install ...

  10. 为什么要使用mybaits

    通常在项目开发过程中,有很多代码是重复的,固定不变的.为了提升开发效率,可将这些 固定不变的代码提取出来,生成class文件,将class文件打jar包,基于框架开发. mybaits是一个ORM框架 ...