【发布时间】:2014-08-04 01:29:33
【问题描述】:
我正在尝试将一些带有图像的自定义数据发布到 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);
}
}
}
现在的问题是,当我使用参数保留此方法并从提琴手发布数据时,它显示 415 Unsupported Media Type error 。如果我删除参数,那么它工作正常。但我确实需要将数据与发布的图片一起传递。
任何人都可以提出一个好的方法来完成这个吗?
谢谢
【问题讨论】:
-
在这里查看这个答案stackoverflow.com/questions/20909118/… - 基本上有很多相同的问题。
-
@Darren 谢谢。我将尝试以这种方式实现它。我只是对这种行为感到好奇。
-
不用担心。最终,当使用 web api 控制器时,您处理文件上传的方式与使用 web 表单 webmethod(或类似方法)非常不同——这将是您大多数问题的根源。 - 这个答案让您了解如何接受文件。您应该能够调整输入参数以满足您的需要。 stackoverflow.com/questions/10320232/…
-
另外 - 作为一个轻微的旁注。确保将内容类型设置为 multipart/form-data
-
@Darren 谢谢。我更改了实现,但如果我保留参数,它的行为方式相同。
标签: asp.net asp.net-web-api image-uploading custom-data-type