如何使用 Servicestack PostFileWithRequest [英] How to use Servicestack PostFileWithRequest

查看:45
本文介绍了如何使用 Servicestack PostFileWithRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to figure out how to post a file to my webservice using servicestack. I have the following code in my client

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)

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

But no matter what I do I get the error message "Method not allowed". At the moment the server code is written like this

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

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

As you can see I have not tried to process the file on the server yet. I just want to test the client to make sure it can actually send the file to the server. Any ideas what I am doing wrong?

推荐答案

首先,您正在使用 ServiceStack 的旧 API,现在已弃用,请考虑迁移到 ServiceStack 的新 API,用于创建未来的服务.

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

您可以查看 ServiceStack 的 RestFiles 示例项目,了解 处理文件上传的示例:

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

可以通过检查注入的 RequestContext 来访问上传文件的集合.

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

上传文件的示例包含在 RestFiles 集成测试:

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

这篇关于如何使用 Servicestack PostFileWithRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆