HttpWebRequest + multipart/form-data
needs boundaries:
By using HttpWebRequest
with multipart/form-data
requires that you specify the boundaries of your content. Which is rather a lot of work and can easily cause corrupt uploads if you don't understand it. However this question here covers how to do it.
Use the PCL ServiceStack.Client:
But as you are using a ServiceStack backend, then the best approach is to use the ServiceStack.Client PCL library in your Android application, which provides the easy to use JsonServiceClient
. See this example for a full Android with ServiceStack demonstration.
So given a simple Upload Service (on your server side):
[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 hope this helps.