web api 处理发送过来的文件(图片)
2016-11-09 22:48
155 查看
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Web; using System.Web.Http; using fastJSON; using System.IO; using System.Net.Http; using DoMain; using System.Web.Configuration; namespace PreAlert_WebService.Controllers { public class Article_Pic_Controller : ApiController { /// <summary> /// 增加图片服务 /// </summary> /// <param name="jsonParames"></param> /// <returns></returns> [HttpPost, HttpGet] public string Pic_Add(string jsonParames) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!")); } HttpRuntimeSection runTime = (HttpRuntimeSection)WebConfigurationManager.GetSection("system.web/httpRuntime"); int maxRequestLength = (runTime.MaxRequestLength) * 1024; int len = System.Web.HttpContext.Current.Request.ContentLength; string retJsonStr = ""; if (string.IsNullOrEmpty(jsonParames) || jsonParames.ToLower() == "jsonparames") { jsonParames = System.Web.HttpContext.Current.Request.Form["jsonParames"]; } IDictionary<string, object> entityDic = (Dictionary<string, object>)JSON.Instance.Parse(jsonParames); string guid = Guid.NewGuid().ToString();//图片唯一ID //string app_path = AppDomain.CurrentDomain.BaseDirectory; string app_path = System.Configuration.ConfigurationManager.AppSettings["app_path"]; string[] needParames = { "PicType", "PicSizeByte" }; //IDictionary<string, object> entityDic = (Dictionary<string, object>)JSON.Instance.Parse(jsonParames); foreach (string needParame in needParames) { if (!entityDic.ContainsKey(needParame)) { retJsonStr = "{\"ret\":\"0\",\"msg\":\"缺少必须的参数:" + needParame + "}"; return retJsonStr; } } //默认png图片格式... string PicType = entityDic["PicType"].ToString().Trim() != "" ? entityDic["PicType"].ToString() : "png"; string Description = entityDic["Description"].ToString(); string PicPath = "/articlepic/" + CollectItemID.ToString(); string PicDir = app_path + PicPath; string FullPath = PicDir + "/" + guid + "." + PicType; int PicSizeByte = 0; int.TryParse(entityDic["PicSizeByte"].ToString(), out PicSizeByte); HttpRequest request = System.Web.HttpContext.Current.Request; HttpFileCollection fileCollection = request.Files; // 判断是否有文件 if (fileCollection.Count > 0) { // 获取图片文件 HttpPostedFile httpPostedFile = fileCollection[0]; // 文件扩展名 string fileExtension = Path.GetExtension(httpPostedFile.FileName); // 验证图片格式 if (fileExtension.Contains(".jpg") || fileExtension.Contains(".png") || fileExtension.Contains(".bmp") || fileExtension.Contains(".gif")) { // 如果目录不存在则要先创建 if (!Directory.Exists(PicDir)) { Directory.CreateDirectory(PicDir); } httpPostedFile.SaveAs(FullPath); using (DBEntities db = new DBEntities()) { Pic apic = new Pic(); apic.PicPath = PicPath + "/" + guid + "." + PicType; apic.PicType = PicType; apic.PicSizeByte = PicSizeByte; db.Pic.Add(apic); db.SaveChanges(); } retJsonStr = "{\"ret\":\"1\",\"msg\":创建成功\"}"; return retJsonStr; } else { retJsonStr = "{\"ret\":\"0\",\"msg\":请选择jpg/png/bmp/gif格式的图片\"}"; } } else { retJsonStr = "{\"ret\":\"0\",\"msg\":图片传输错误:没有发现要上传的图片;\"}"; } retJsonStr = "{\"ret\":\"1\",\"msg\":创建成功\"}"; return retJsonStr; } } }
相关文章推荐
- php处理微信小程序wx.uploadFile(OBJECT)发送过来的文件
- ASP.NET SignalR 与 LayIM2.0 配合轻松实现Web聊天室(三) 之 实现单聊,群聊,发送图片,文件。
- WebSocket发送图片或文件的思路(2)
- C/C++-----------http协议发送字段,文件,单个和多张图片
- Qt实现Socket从文件发送多幅图片(Qt③)
- 文件上传类和图片处理类
- ajax 请求二进制流 图片 文件 XMLHttpRequest 请求并处理二进制流数据 之最佳实践
- MVC、一般处理程序hanlder 输出图片文件
- 关于apache-activemq发送图片未处理的解决方案
- 微信公共服务平台开发(.Net 的实现)8-------处理图片(上传下载发送)
- 七牛云存储高速处理图片等文件
- 一堆图片文件的读取/处理后进行顺序存储
- 使用express搭建了框架后,用multer处理post请求传的文件或图片注意的问题
- 静态库资源文件、xib、图片打包处理,编译生成的静态库.a包文件太大缩小解决方法
- c# Aspose 图片处理(将Excel单元格中的图片拷贝到另一个Excel文件中)
- ASP.NET2.0文件图片处理
- 实例详解Nodejs 保存 payload 发送过来的文件
- ASP.NET2.0文件上传以及图片处理
- 【Android】图片/文件的处理方法
- python3.6深度学习图片集处理项目源代码(为cnn等准备图片集合,批量生成、删除、改名、旋转、调整图片大小,包括删除指定路径下所有文件)