0

I'm trying to post an image to my server using httpclient on Monodroid. The server-code is ok, infact using Postman all goes well.

This is my code:

                var req = new HttpRequestMessage (System.Net.Http.HttpMethod.Post, "http://192.168.0.50:2345/homo");

            var content = new MultipartFormDataContent ();
            var imageContent = new StreamContent (new FileStream ("my_path.jpg", FileMode.Open, FileAccess.Read, FileShare.Read));
            imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse ("image/jpeg");

            content.Add (imageContent, "image", "image.jpg");
            req.Content = content;
            await client.SendAsync (req);

When I execute this code, on the server side I get this image:

enter image description here

So, like you can see, something comes... but it is not the complete file.

Can you help me?

Thanks a lot!

user3471528
  • 3,013
  • 6
  • 36
  • 60

1 Answers1

1

HttpClient buffers 64k, which probably equals to the data that is shown on the other side. To have it send the file in chunks do something like:

var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.TransferEncodingChunked = true;

var content = new MultipartFormDataContent ();
var imageContent = new StreamContent (new FileStream ("my_path.jpg", FileMode.Open, FileAccess.Read, FileShare.Read));
imageContent.Headers.ContentType = MediaTypeHeaderValue.Parse ("image/jpeg");
content.Add(imageContent, "image", "image.jpg");

await httpClient.PostAsync(url, content);

However, you don't need the multipart if you are only sending one image...

Cheesebaron
  • 24,131
  • 15
  • 66
  • 118