0

How would one post multiple binaries in a single http POST operation using C# httpclient? I can't seem to find information on how to deal with httpcontent in this way - just doing a postASync with stream data twice?

JosephA
  • 113
  • 2
  • 9

2 Answers2

1

Dug around a bit more and experimented, and finally found what seems like a working solution. I tried this on a test server with some images on HD - both sent, both worked. With two stream examples.

        var client = new HttpClient();

        var stream3 = new FileStream("saved.jpg", FileMode.Open);
        var stream2 = new FileStream("saved2.jpg", FileMode.Open);

        var dic = new Dictionary<string, string>();
        dic.Add("Test1", "This was the first test.");

        var addy = "http://posttestserver.com/post.php";

        using (var content = new MultipartFormDataContent())
        {
            content.Add(new StreamContent(stream2), "s1", "Saved1.jpg");
            content.Add(new StreamContent(stream3), "s2", "Saved2.jpg");

            var response = await client.PostAsync(addy, content);
            response.EnsureSuccessStatusCode();

            string finalresults = await response.Content.ReadAsStringAsync();
        }
JosephA
  • 113
  • 2
  • 9
0

It will depend on the implementation of the API that you are sending your files to but typically if multiple files are sent in a single POST request then it is sent as multipart/form-data. Have a look at this post for sending multipart/form-data through HttpClient.

Community
  • 1
  • 1
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29