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

该案例是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. 保存数据到Excel中

    调用的方法传值 Export(dt, "Cal_Report_" + DateTime.Now.ToString("yyyyMMddhhmmss") + &qu ...

  2. Java产生死锁的一个简单例子

    思路是创建两个字符串a和b,再创建两个线程A和B,让每个线程都用synchronized锁住字符串(A先锁a,再去锁b:B先锁b,再锁a),如果A锁住a,B锁住b,A就没办法锁住b,B也没办法锁住a, ...

  3. 机器学习算法中的准确率(Precision)、召回率(Recall)、F值(F-Measure)

    摘要: 数据挖掘.机器学习和推荐系统中的评测指标—准确率(Precision).召回率(Recall).F值(F-Measure)简介. 引言: 在机器学习.数据挖掘.推荐系统完成建模之后,需要对模型 ...

  4. centos实现永久修改hostname

    前言 介绍一下centos的两种修改hostname的方式. 查看hostname [root@slave02 ~]# hostname slave02 临时性修改 [root@slave02 ~]# ...

  5. ABAP-表中数据的横向累加

    GLT0 等表中数据的横向累加 ——塞依SAP培训   在 ABAP 的 GLT0.FAGLFLEXT 等余额表中,用诸如 TSL01.TSL02 …… TSL16 等字段记录了 16 个期间的发生额 ...

  6. ABAP-FTP-执行

    1.界面 2.程序 ZFID0004_FTP_EXEC 主程序: *&------------------------------------------------------------- ...

  7. vue 全局组件【原】

    1.目录 2.内容 -Loading.vue <template> <div class="loading"> loading... </div> ...

  8. C++ 下面的AIDL

    转自https://android.googlesource.com/platform/system/tools/aidl/+/brillo-m10-dev/docs/aidl-cpp.md. Bac ...

  9. 《DOM Scripting》学习笔记-——第三章 DOM

    <Dom Scripting>学习笔记 第三章 DOM 本章内容: 1.节点的概念. 2.四个DOM方法:getElementById, getElementsByTagName, get ...

  10. jQuery的appendTo案例

    案例要求:点击双击第一个下拉列表框的选项可以把对应选项移到第二个下拉列表框中,选中第一个列表框的选项(可多选)单击-->按钮可使被选中项移动到右边下拉列表框中,单击==>按钮时将左边的所有 ...