2

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?

Marc Climent
  • 9,434
  • 2
  • 50
  • 55

1 Answers1

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));
mythz
  • 141,670
  • 29
  • 246
  • 390
  • I do plan to move to the new style API but for now I need a quick fix to this problem. For instance the example does not show me how to use the PostFileWithRequest or why I am getting the "Method now allowed". Is there anything I am doing wrong in my code using the old style API? – Michael Josiah Dec 16 '12 at 11:16
  • If you're getting 405's Method Not Allowed you may need to [remove WebDav](http://stackoverflow.com/a/10283694/85785) – mythz Dec 17 '12 at 05:15
  • This does not work for me. I added the as instructed but still get the method not allowed. Anyone have anymore ideas? – Michael Josiah Dec 29 '12 at 15:05