2

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

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)

但无论我做什么,我都会收到错误消息“方法不允许”。目前服务器代码是这样写的

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

如您所见,我还没有尝试处理服务器上的文件。我只是想测试客户端以确保它实际上可以将文件发送到服务器。任何想法我做错了什么?

4

1 回答 1

1

Firstly you're using ServiceStack's Old and now deprecated API, consider moving to ServiceStack's New API for creating future services.

You can look at ServiceStack's RestFiles example project for an example of handling file uploads:

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

Which is able to access the collection of uploaded files by inspecting the injected RequestContext.

An example of uploading a file is contained in the 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));
于 2012-12-16T11:10:43.913 回答