3

I'm struggling with this issue for several hours, and I can't find any solution. Does someone used ServiceStack to upload multiple files with one POST request?

I was trying to use PostFile:

  FileInfo fi = new FileInfo("ExampleData\\XmlAPI.xml");
  var client = new XmlServiceClient("http://localhost:1337");
  client.PostFile<DefaultResponse>("/test", fi, "application/xml");

But here I am able to add only one file to the request. My second shot was to use LocalHttpWebRequestFilter but inside there is only a extension method which also allows to post only one file.

user1214919
  • 384
  • 4
  • 14

1 Answers1

2

Multiple File Upload APIs have been added to all .NET Service Clients in v4.0.54 that allow you to easily upload multiple streams within a single HTTP request. It supports populating Request DTO with any combination of QueryString and POST'ed FormData in addition to multiple file upload data streams:

using (var stream1 = uploadFile1.OpenRead())
using (var stream2 = uploadFile2.OpenRead())
{
    var client = new JsonServiceClient(baseUrl);
    var response = client.PostFilesWithRequest<MultipleFileUploadResponse>(
        "/multi-fileuploads?CustomerId=123",
        new MultipleFileUpload { CustomerName = "Foo,Bar" },
        new[] {
            new UploadFile("upload1.png", stream1),
            new UploadFile("upload2.png", stream2),
        });
}

Or using only a Typed Request DTO. The JsonHttpClient also includes async equivalents for each of the new PostFilesWithRequest APIs:

using (var stream1 = uploadFile1.OpenRead())
using (var stream2 = uploadFile2.OpenRead())
{
    var client = new JsonHttpClient(baseUrl);
    var response = await client.PostFilesWithRequestAsync<MultipleFileUploadResponse>(
        new MultipleFileUpload { CustomerId = 123, CustomerName = "Foo,Bar" },
        new[] {
            new UploadFile("upload1.png", stream1),
            new UploadFile("upload2.png", stream2),
        });
}
mythz
  • 141,670
  • 29
  • 246
  • 390
  • New comment on an old question/answer, but I found [this gist](https://gist.github.com/cakriwut/fab729cad785047cc8f0) which adds in extension methods to the `ServiceClientBase` to POST multiple files with a request body. Looks like the author hand-rolled logic. – jklemmack Jan 18 '17 at 19:23
  • @jklemmack thx for the nudge :) updated answer to include new Multiple File Upload APIs added in v4.0.54. – mythz Jan 18 '17 at 19:29
  • 1
    Oh Google, you have failed me! *wails* *gnashes teeth* – jklemmack Jan 18 '17 at 19:30