User image api web image

I am trying to post some custom image data on a web api. Please take a look at the method below.

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);
            }
        }
    }

Now the problem is that I save this method with the parameter and publish data from fiddler, it shows me 415 Error of unsupported media type . If I remove the parameter, it works fine. But I really need to transfer the data along with the posted image.

Can anyone suggest a good way to do this?

thank

+4
source share
1 answer

. . , -.

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;
    }

. mutipart-formdata.

+3

Source: https://habr.com/ru/post/1544481/


All Articles