ServiceStack - Send multiple files with one POST request

I have been struggling with this problem for several hours and I cannot find any solution. Has anyone used ServiceStack to upload multiple files with one POST request?

I tried 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 can only add one file to the request. My second shot was to use LocalHttpWebRequestFilter, but inside there is only an extension method that also only allows one file to be posted.

+3


source to share


1 answer


Several file upload APIs were added to all .NET Service Clients in version 4.0.5, which make it easy to download multiple streams within a single HTTP request. It supports populating a DTO request with any combination of QueryString and POST'ed FormData in addition to multiple file transfer 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 just the Generic DTO Request. JsonHttpClient

also includes asynchronous 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),
        });
}

      

+2


source







All Articles