2014-09-28 64 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