我正在尝试创建一个用于接受图像的API。这是我的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);
当我尝试使用Postman
上传时,它正常工作并保存图像。但是,我正在尝试使用HttpClient
上传图片我收到错误Unsupported Media Type
以下是上传图片的代码。
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;
有没有解决方案?