1

I am trying to create an API for Accepting image. Here is my code for the API.

var image = Image.FromStream(postedFile.InputStream, true, true);
Rectangle cropSize = new Rectangle((image.Width / 2 - 250), (image.Height / 2 - 250), 500, 500);
var bmp = new Bitmap(cropSize.Width, cropSize.Height);
using (var gr = Graphics.FromImage(bmp))
{
    gr.DrawImage(image, new Rectangle(0, 0, bmp.Width, bmp.Height), cropSize, GraphicsUnit.Pixel);
}
bmp.Save(ImagePath);

It's working fine and an image is being saved when I try to upload using Postman. However, I am trying to upload image using HttpClient I am getting error Unsupported Media Type

Here is the code for uploading Image.

HttpClient client = new HttpClient();
MultipartFormDataContent form = new MultipartFormDataContent();
HttpContent content = new StringContent("fileToUpload");
form.Add(content, "fileToUpload");
content = new StreamContent(file.GetStream());
content.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data")
{
    Name = "fileToUpload",
    FileName = "MIS1001-demo-xyz.png"
};
form.Add(content);
var response = await client.PostAsync(Constants.ApiBaseURL + Constants.ApiEndPoint.UpdateProfile, form);
return response.Content.ReadAsStringAsync().Result;

Is there any solution for this?

Chandresh Khambhayata
  • 1,748
  • 2
  • 31
  • 60

0 Answers0