I'm trying to upload an image to a ASP.NET RESTful Web API from an Android client. To accomplish this, I'm using the Android Asynchronous Http Library.
I'm expecting a header to look like the one mentioned in this post.
POST http://localhost:50460/api/values/1 HTTP/1.1
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip, deflate
Content-Type: multipart/form-data; boundary=---------------------------41184676334
Content-Length: 29278
-----------------------------41184676334
Content-Disposition: form-data; name="caption"
Summer vacation
-----------------------------41184676334
Content-Disposition: form-data; name="image1"; filename="GrandCanyon.jpg"
Content-Type: image/jpeg
(Binary data not shown)
-----------------------------41184676334--
My problem is that the request I make is not a multipart request and so I always throw the unsupported media type exception.
Below is my .NET code.
public Task<Guid> Upload([)
{
Guid userGuid;
// Check if the request contains multipart/form-data
// or if the extension is valid
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
return null;
}
var provider = new CustomMultipartFormDataStreamProvider(UPLOAD_PATH);
// Read the form data and return an async task.
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<Guid>(t =>
{
// mode code
foreach (MultipartFileData file in provider.FileData)
{
filename = file.Headers.ContentDisposition.FileName;
// ... more code
}
return guid;
});
return task;
}
I've also tried something like this: Request.Content.IsMimeMultipartContent("form-data")
I created an HTML client using jQuery and I'm able to upload to the REST service without any problems. This leads me to believe that there is something I'm missing in my Android code. This is what my Java method looks like:
File file = new File([path to your file]);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = FilenameUtils.getExtension(file.getName());
String mime_type = map.getMimeTypeFromExtension(ext);
MultipartEntity form = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, this);
form.addPart("file-data", new FileBody(file, mime_type, "UTF-8"));
AsyncHttpClient client = new AsyncHttpClient();
client.post(context, [your url], form, mime_type, responseHandler) ;
I'm not sure what I'm missing. I've been stuck at this for two days now and I've tried various different options. Top is just the very basic version I found based on this post.
I was wondering if I can get some directions and hints on how I should go about resolving this issue.
Thanks!