C# 最齐全的上传图片方法
来源:本站原创|时间:2020-01-10|栏目:C#教程|点击: 次
方法里包括了图片大小限制、图片尺寸、文件内容等等的判断。。。
该案例是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的大小!"); } }
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持我们!
您可能感兴趣的文章
- 01-10C#通过反射获取当前工程中所有窗体并打开的方法
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#停止线程的方法
- 01-10WinForm实现仿视频播放器左下角滚动新闻效果的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的方法
- 01-10C#实现清空回收站的方法
- 01-10C#实现读取注册表监控当前操作系统已安装软件变化的方法
- 01-10C#实现多线程下载文件的方法
阅读排行
本栏相关
- 01-10C#通过反射获取当前工程中所有窗体并
- 01-10关于ASP网页无法打开的解决方案
- 01-10WinForm限制窗体不能移到屏幕外的方法
- 01-10WinForm绘制圆角的方法
- 01-10C#实现txt定位指定行完整实例
- 01-10WinForm实现仿视频播放器左下角滚动新
- 01-10C#停止线程的方法
- 01-10C#实现清空回收站的方法
- 01-10C#通过重写Panel改变边框颜色与宽度的
- 01-10C#实现读取注册表监控当前操作系统已
随机阅读
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-11ajax实现页面的局部加载
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10delphi制作wav文件的方法
- 01-10C#中split用法实例总结
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 04-02jquery与jsp,用jquery
- 01-10SublimeText编译C开发环境设置