I am trying to upload an image to an ASP.NET RESTful Web API from an Android client. For this, I use the Asynchronous Http Android library .
I expect the title to be similar to the title.
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 am making is not multi-page, and therefore I always throw an unsupported media type exception.
Below is my .NET code.
public Task<Guid> Upload([)
{
Guid userGuid;
if (!Request.Content.IsMimeMultipartContent())
{
throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
return null;
}
var provider = new CustomMultipartFormDataStreamProvider(UPLOAD_PATH);
var task = Request.Content.ReadAsMultipartAsync(provider).
ContinueWith<Guid>(t =>
{
foreach (MultipartFileData file in provider.FileData)
{
filename = file.Headers.ContentDisposition.FileName;
}
return guid;
});
return task;
}
I also tried something like this: Request.Content.IsMimeMultipartContent("form-data")
I created the HTML client using jQuery and I can easily load it into the REST service. It makes me believe that there is something in my Android code that I donβt see. 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) ;
, . , . Top - , .
, , .
!