如何使用Servicestack PostFileWithRequest

时间:2012-12-16 10:41:00

标签: servicestack

我想弄清楚如何使用servicestack将文件发布到我的webservice。我的客户端中有以下代码

Dim client As JsonServiceClient = New JsonServiceClient(api)
Dim rootpath As String = Server.MapPath(("~/" + "temp"))
Dim filename As String = (Guid.NewGuid.ToString.Substring(0, 7) + FileUpload1.FileName)

rootpath = (rootpath + ("/" + filename))

FileUpload1.SaveAs(rootpath)

Dim fileToUpload = New FileInfo(rootpath)
Dim document As AddIDVerification = New AddIDVerification

document.CountryOfIssue = ddlCountry.SelectedValue
document.ExpiryDate = DocumentExipiry.SelectedDate
document.VerificationMethod = ddlVerificationMethod.SelectedValue

Dim responseD As MTM.DTO.AddIDVerificationResponse = client.PostFileWithRequest(Of DTO.AddIDVerificationResponse)("http://localhost:50044/images/", fileToUpload, document)

但无论我做什么,我都会收到错误消息“Method not allowed”。目前,服务器代码就是这样编写的

Public Class AddIDVerificationService
    Implements IService(Of DTO.AddIDVerification)

    Public Function Execute(orequest As DTO.AddIDVerification) As Object Implements ServiceStack.ServiceHost.IService(Of DTO.AddIDVerification).Execute
        Return New DTO.AddIDVerificationResponse With {.Result = "success"}
    End Function
End Class

正如您所看到的,我还没有尝试过在服务器上处理该文件。我只是想测试客户端以确保它实际上可以将文件发送到服务器。我有什么想法吗?

1 个答案:

答案 0 :(得分:1)

首先,您使用的是ServiceStack的旧版,现已弃用的API,请考虑转移到ServiceStack's New API以创建将来的服务。

您可以查看ServiceStack's RestFilesexample of handling file uploads示例项目:

foreach (var uploadedFile in base.RequestContext.Files)
{
    var newFilePath = Path.Combine(targetDir.FullName, uploadedFile.FileName);
    uploadedFile.SaveTo(newFilePath);
}

通过检查注入的RequestContext

,可以访问上传文件的集合

上传文件的示例包含在RestFiles integration tests

var client = new JsonServiceClient(api);
var fileToUpload = new FileInfo(FilesRootDir + "TESTUPLOAD.txt");
var response = restClient.PostFile<FilesResponse>(
    "files/Uploads/",fileToUpload,MimeTypes.GetMimeType(fileToUpload.Name));