ServiceStack - 使用一个POST请求发布多个文件

时间:2013-02-19 09:40:27

标签: post servicestack multifile-uploader

我在这个问题上挣扎了几个小时,我找不到任何解决方案。 有人使用ServiceStack通过一个POST请求上传多个文件吗?

我试图使用PostFile:

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

但在这里,我只能在请求中添加一个文件。 我的第二个镜头是使用LocalHttpWebRequestFilter,但里面只有一个扩展方法,它也只允许发布一个文件。

1 个答案:

答案 0 :(得分:2)

多个文件上传API added to all .NET Service Clients in v4.0.54允许您在单个HTTP请求中轻松上传多个流。它支持使用QueryString和的任意组合填充Request DTO 除多个文件上传数据流外,还发布了FormData:

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

或仅使用Typed Request DTO。 JsonHttpClient还包括每个新的异步等价物 PostFilesWithRequest API:

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