Click here to Skip to main content
16,008,299 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I've tried send image through multipart/form POST method to server.
However, send file is working. With POSTMAN, with C# code, it worked.
But I want send Bitmap(Image), not file.

with this code, it worked.

HttpClient client = new HttpClient())
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "some url");
request.Headers.Add("apiKey", "some key");
var content = new MultipartFormDataContent();
content.Add(new StringContent("TEST IMAGE"), "ImageName");
content.Add(new StreamContent(File.OpenRead("C:/Test/Test.jpg")), "Image", "Test.jpg");
content.Add(new StringContent("IMAGE CODE"), "Code");
request.Content = content;

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();

What I have tried:

This code always get 400 bad request error.
What I've done wrong? How can I achieve send image, not file?

public static Stream ImageToStream(Image image, ImageFormat format)
{
var stream = new System.IO.MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}


HttpClient client = new HttpClient())
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "some url");
request.Headers.Add("apiKey", "some key");
var content = new MultipartFormDataContent();
content.Add(new StringContent("TEST IMAGE"), "ImageName");
var imageContent = new StreamContent(ImageToStream(myImage, ImageFormat.jpeg));
content.Add(imageContent, "Image");
content.Add(new StringContent("IMAGE CODE"), "Code");
request.Content = content;

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Posted
Updated 30-Aug-24 15:34pm
v2

1 solution

It solved. I think this API server is expect user to only upload file, not Image. So I change my code for add filename parameter, and it works.

===========================================================================

public static Stream ImageToStream(Image image, ImageFormat format)
{
var stream = new System.IO.MemoryStream();
image.Save(stream, format);
stream.Position = 0;
return stream;
}

HttpClient client = new HttpClient())
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, "some url");
request.Headers.Add("apiKey", "some key");
var content = new MultipartFormDataContent();
content.Add(new StringContent("TEST IMAGE"), "ImageName");
var imageContent = new StreamContent(ImageToStream(myPictureBox.Image, ImageFormat.jpeg));
content.Add(imageContent, "Image", "Test.jpg"); // This is it!
content.Add(new StringContent("IMAGE CODE"), "Code");
request.Content = content;

HttpResponseMessage response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900