【问题标题】:How to use Servicestack PostFileWithRequest如何使用 Servicestack PostFileWithRequest
【发布时间】:2012-12-03 17:47:00
【问题描述】:

我想弄清楚如何使用 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

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

【问题讨论】:

    标签: servicestack


    【解决方案1】:

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

    您可以查看ServiceStack's RestFiles 示例项目以获取example 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));
    

    【讨论】:

    • 我确实计划迁移到新风格的 API,但现在我需要快速解决这个问题。例如,该示例没有向我展示如何使用 PostFileWithRequest 或为什么我得到“现在允许的方法”。使用旧式 API 在我的代码中我做错了什么吗?
    • 如果您收到 405 的方法不允许,您可能需要 remove WebDav
    • 这对我不起作用。我按照指示添加了 但仍然得到不允许的方法。还有人有什么想法吗?
    猜你喜欢
    • 1970-01-01
    • 2015-01-29
    • 1970-01-01
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多