ServiceStack的“新API”中的PostFileWithRequest等价物是什么?

时间:2013-03-06 17:09:06

标签: servicestack

我想在multipart-formdata文件内容旁边发布一些请求值。在旧的API中,您可以使用PostFileWithRequest:

[Test]
public void Can_POST_upload_file_using_ServiceClient_with_request()
{
    IServiceClient client = new JsonServiceClient(ListeningOn);
    var uploadFile = new FileInfo("~/TestExistingDir/upload.html".MapProjectPath());
    var request = new FileUpload{CustomerId = 123, CustomerName = "Foo"};

    var response = client.PostFileWithRequest<FileUploadResponse>(ListeningOn + "/fileuploads", uploadFile, request);

    var expectedContents = new StreamReader(uploadFile.OpenRead()).ReadToEnd();

    Assert.That(response.FileName, Is.EqualTo(uploadFile.Name));
    Assert.That(response.ContentLength, Is.EqualTo(uploadFile.Length));
    Assert.That(response.Contents, Is.EqualTo(expectedContents));
    Assert.That(response.CustomerName, Is.EqualTo("Foo"));
    Assert.That(response.CustomerId, Is.EqualTo(123));
}

我在新的API中找不到任何这样的方法,也没有在client.Post()上找到任何覆盖,这表明这仍然是可能的。有谁知道这是否是一个被删除的功能?

更新

正如@Mythz指出的那样,该功能并未被删除。我犯了不投客户的错误:

private IRestClient CreateRestClient()
{
    return new JsonServiceClient(WebServiceHostUrl);
}

[Test]
public void Can_WebRequest_POST_upload_binary_file_to_save_new_file()
{
    var restClient = (JsonServiceClient)CreateRestClient(); // this cast was missing
    var fileToUpload = new FileInfo(@"D:/test/test.avi");
    var beforeHash = this.Hash(fileToUpload);

    var response = restClient.PostFileWithRequest<FilesResponse>("files/UploadedFiles/", fileToUpload, new TestRequest() { Echo = "Test"});

    var uploadedFile = new FileInfo(FilesRootDir + "UploadedFiles/test.avi");
    var afterHash = this.Hash(uploadedFile);

    Assert.That(beforeHas, Is.EqualTo(afterHash));
}

private string Hash(FileInfo file)
{
    using (var md5 = MD5.Create())
    {
        using (var stream = file.OpenRead())
        {
            var bytes = md5.ComputeHash(stream);
            return BitConverter.ToString(md5.ComputeHash(stream)).Replace("-", "").ToLower();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

C# Service Clients中删除了旧的API,只添加了新的API。

您在服务中处理上传文件的方式也没有改变。