0

I used the following code from the answer to this question by @scott How do I upload an image to a ServiceStack service?

[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" }});
}

I used this with my DTO and in my Android app, but when I try to send it always fails with the following server error:

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

Can anyone share Monodroid ServiceStack Image upload sample?

Thanks.

Community
  • 1
  • 1
Hâluk Yilmaz
  • 237
  • 5
  • 12

1 Answers1

2

There is nothing wrong with the example code, that you have taken from my answer given here, which you used in the Monodroid client. It works on Monodroid using the ServiceStack PCL library without modification.

Monodroid:

No modification required.

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" }});
}

Server File Permissions Error:

The error message you get when you upload to the ServiceStack service shows that your server process does not have permission to write the file to this directory C:\Windows\SysWOW64\inetsrv.

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

You need to update the server side service to write the file to a path which the service has permission to.

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;
    }
}

If you fix the permission error you will see it works.

Community
  • 1
  • 1
Scott
  • 21,211
  • 8
  • 65
  • 72
  • allright, currently i can not access to server side, tomorrow i will try it ( write / update auth from windows server ) thanks, i was whole lost my time while try old pcl libraries, i just updated and get this connection info anyway thanks – Hâluk Yilmaz Sep 28 '14 at 17:05
  • why did you -1 my question? – Hâluk Yilmaz Sep 28 '14 at 17:05
  • @HâlukYilmaz I -1 because your question lacked formatting and capitalisation of words. Furthermore the request for specific code examples is off topic and the error was indicated in the error message with `Path denied`. However I have formatted your question for you and removed the downvote. – Scott Sep 28 '14 at 17:15
  • ok it worked :) now i will try to load it from temp directory is it easy as upload? and second question is http://212.175.132.168/next/api/json/metadata?op=CityReportsAdd this is my add class, so can i call same time or should i upload twice ? thanks for advice from now – Hâluk Yilmaz Oct 07 '14 at 11:35