2014-09-28 45 views
0

我通過@scott使用從答案下面的代碼對這個問題How do I upload an image to a ServiceStack service?當上傳圖片/文件服務器,ServiceStack將引發UnauthorizedAccessException

[Route("/upload","POST")] 
public class UploadFileRequest 
{ 
    // Example of other properties you can send with the request 
    public string[] Tags { get; set; } 
} 

class MyFileService : Service 
{ 
    public bool Post(UploadFileRequest request) 
    { 
     // Check a file has been attached 
     if(Request.Files == null || Request.Files.Length == 0) 
      throw new HttpError(400, "Bad Request", "No file has been uploaded"); 

     // Save the file 
     Request.Files[0].SaveTo(Request.Files[0].FileName); 

     // Maybe store the tags (or any other data in the request) 
     // request.Tags 

     return true; 
    } 
} 

Then with the JsonServiceClient in your Android app, then your simply need to do this:

var filename = "cab.jpg"; // The path of the file to upload 
var client = new JsonServiceClient("http://212.175.132.168/service/api/"); 
using(var fileStream = File.OpenRead(filename)) 
{ 
    client.PostFileWithRequest<bool>(fileStream, "cab.jpg", new UploadFileRequest { Tags = new[] { "Cab", "Taxis", "NewYork", "Yellow" }}); 
} 

我用這與我的DTO並在我的Android應用程序,但當我嘗試發送它總是失敗,並出現以下服務器錯誤:

{"ResponseStatus": {"ErrorCode":"UnauthorizedAccessException","Message":"'C:\\Windows\\SysWOW64\\inetsrv\\a.png' path denied.", 'C:\Windows\SysWOW64\inetsrv\a.png' path denied. 

任何人都可以分享Monodroid ServiceStack圖片上傳樣本?

謝謝。

回答

2

沒有什麼錯示例代碼,that you have taken from my answer given here,您在客戶端MonoDroid的使用。它可以在沒有修改的情況下使用ServiceStack PCL庫在Monodroid上工作。

Monodroid:

無需任何修改。

var filename = "cab.jpg"; // The path of the file to upload 
var client = new JsonServiceClient("http://212.175.132.168/service/api/"); 
using(var fileStream = File.OpenRead(filename)) 
{ 
    client.PostFileWithRequest<bool>(fileStream, "cab.jpg", new UploadFileRequest { Tags = new[] { "Cab", "Taxis", "NewYork", "Yellow" }}); 
} 

服務器文件權限錯誤:

當你上傳到ServiceStack服務,您收到錯誤消息表明您的服務器進程沒有權限將文件寫入此目錄C:\Windows\SysWOW64\inetsrv

{ 
    "ResponseStatus":  
    { 
     "ErrorCode":"UnauthorizedAccessException", 
     "Message":"'C:\Windows\SysWOW64\inetsrv\a.png' path denied." 
    } 
} 

您需要更新服務器端服務以將文件寫入服務有權訪問的路徑。

class MyFileService : Service 
{ 
    public bool Post(UploadFileRequest request) 
    { 
     // Check a file has been attached 
     if(Request.Files == null || Request.Files.Length == 0) 
      throw new HttpError(400, "Bad Request", "No file has been uploaded"); 

     // Replace with a path you have permission to write to 
     var path = @"c:\temp\image.png"; 

     // Save the file 
     Request.Files[0].SaveTo(path); 

     // Maybe store the tags (or any other data in the request) 
     // request.Tags 

     return true; 
    } 
} 

如果您修復權限錯誤,您將看到它的工作原理。

+0

好吧,我目前無法訪問到服務器端,明天我會嘗試(從Windows Server寫入/更新AUTH)謝謝,我是全失去了我的時間,同時嘗試老PCL庫,我剛剛更新,並得到這方面的信息無論如何,謝謝 – 2014-09-28 17:05:17

+0

你爲什麼-1我的問題? – 2014-09-28 17:05:51

+0

@HâlukYilmaz不客氣。我希望這會順利。 – Scott 2014-09-28 17:07:44