【问题标题】:web api post image with custom data带有自定义数据的 web api 发布图像
【发布时间】: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


【解决方案1】:

此行还获取一个 post 参数以及上传的文件:

string value = HttpContext.Current.Request.Params.Get("key");

【讨论】:

    【解决方案2】:

    经过大量搜索后,我想出了将数据与图像一起上传的方法。这是代码的修改版本。希望对某人有所帮助。

    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), new PointF(0, 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;
        }
    

    注意:应将数据发布为多部分形式数据。

    【讨论】:

    • 您是如何使用此解决方案读取自定义 FlyerDetails 类型(数据)的?现在好像只在读取上传的图片?
    • @Aaron 如果您仔细查看代码,现在您将能够使用 streamProvider 获取数据,就像表单集合一样。 streamProvider.FormData["FontFamily"]
    • 好的,谢谢。你知道这是否适用于模型绑定? IE。我可以以这种方式发布自定义类型,并让 .NET 模型将自定义类型绑定回 streamProvider 之外的 .NET 对象,同时获取二进制图像数据?
    • @Aaron 我认为这应该是可能的。即如果您发布自定义对象,那么您应该能够得到它。就我而言,我正在使用 streamProvider 获取控件的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-28
    • 1970-01-01
    • 1970-01-01
    • 2012-06-04
    • 2020-10-28
    • 1970-01-01
    相关资源
    最近更新 更多