2012-12-16 54 views
2

我想弄清楚如何使用servicestack將文件發佈到我的webservice。我有下面的代碼在我的客戶如何使用Servicestack PostFileWithRequest

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 

正如你所看到的,我還沒有嘗試過處理服務器上的文件。我只是想測試客戶端,以確保它實際上可以將文件發送到服務器。任何想法我做錯了什麼?

回答

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

我確實打算在移動到新的風格的API,但現在我需要一個快速的解決這個問題。例如,該示例沒有告訴我如何使用PostFileWithRequest或者爲什麼我得到「現在允許的方法」。我的代碼中使用舊式API有什麼問題嗎? –

+0

如果您得到405的方法不允許,您可能需要[刪除WebDav](http://stackoverflow.com/a/10283694/85785) – mythz

+0

這對我不起作用。按照指示添加了,但仍然不允許使用該方法。任何人有任何想法? –

相關問題