asp.net – 带有自定义数据的web api帖子图片
发布时间:2020-12-16 09:54:25 所属栏目:asp.Net 来源:网络整理
导读:我试图将一些带图像的自定义数据发布到web api.请看下面的方法. public void Post(FlyerDetails FlyerDetails) { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count 0) { foreach (string file in httpRequest.Files) { var file
我试图将一些带图像的自定义数据发布到web api.请看下面的方法.
public void Post(FlyerDetails FlyerDetails) { var httpRequest = HttpContext.Current.Request; if (httpRequest.Files.Count > 0) { foreach (string file in httpRequest.Files) { var filePath = HttpContext.Current.Server.MapPath("~/Flyers/" + httpRequest.Files[file].FileName); Bitmap bmp = new Bitmap(httpRequest.Files[file].InputStream); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; g.DrawString(FlyerDetails.Message,new Font(FlyerDetails.FontColor,FlyerDetails.FontSize),Brushes.DarkRed,new PointF(0,0)); g.Flush(); bmp.Save(filePath); } } } 现在的问题是,当我使用参数保留此方法并从fiddler发布数据时,它显示415不支持的媒体类型错误.如果我删除参数,那么它工作正常.但我真的需要将数据与发布的图像一起传递. 任何人都可以建议一个很好的方法来实现这一目标 谢谢 解决方法
经过大量搜索后,我想出了将数据与图像一起上传的方法.这是代码的修订版本.希望它对某人有帮助.
public async Task<HttpResponseMessage> Post() { var streamProvider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Flyers/")); await Request.Content.ReadAsMultipartAsync(streamProvider); var response = Request.CreateResponse(HttpStatusCode.Created); var filePath = "";// file path if (System.IO.File.Exists(filePath)) { string extension = Path.GetExtension(filePath); Bitmap bmp = new Bitmap(filePath); Graphics g = Graphics.FromImage(bmp); g.SmoothingMode = SmoothingMode.AntiAlias; g.InterpolationMode = InterpolationMode.HighQualityBicubic; g.PixelOffsetMode = PixelOffsetMode.HighQuality; Color brushColor = System.Drawing.ColorTranslator.FromHtml(streamProvider.FormData["FontColorCode"]); g.DrawString(streamProvider.FormData["FontFamily"],new Font(brushColor.Name,Convert.ToInt32(streamProvider.FormData["FontSize"])),new SolidBrush(brushColor),0)); g.Flush(); bmp.Save(HttpContext.Current.Server.MapPath("~/" + Guid.NewGuid() + extension)); response.Headers.Location = new Uri(new Uri(HttpContext.Current.Request.Url.AbsoluteUri).GetLeftPart(UriPartial.Authority) + "/Flyers/" + Guid.NewGuid() + extension); } return response; } 注意:应该将数据发布为mutipart-formdata. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- asp.net core后台系统登录的快速构建
- asp.net-mvc-3 – MVC 3客户端验证,模型绑定十进制值和文化
- asp.net – 横幅宽度不受浏览器大小的限制
- asp.net-mvc – 如何从ASP MVC5中的属性路由获取路由名称
- 第11章 ASP.NET Web API
- asp.net-mvc-2 – 电子商店中类别名称的MVC 2.0动态路由
- asp.net-mvc – 如何在asp.net mvc中使用jquery设置@ Html.
- ASP.NET MVC 4 EF5与MySQL
- asp.net-mvc-4 – autofac – 依赖注入IHttpModule
- 在dotnet发布后运行角度生产版本