When uploading an image/file to server, ServiceStack throws a UnauthorizedAccessException

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.

Answers


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.


Need Your Help

xslt xpath finding all element values of a node in xpath query

xml xslt xpath xml-parsing xslt-1.0

This question is related with this question xslt xpath using element values in xpath query however, the xslt is only for two element nodes HAS TAXONOMIC LEVEL and IS USED AS.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.