问题
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?
回答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));
来源:https://stackoverflow.com/questions/13900473/how-to-use-servicestack-postfilewithrequest