I am trying to upload an image from my android application on the server through a wcf webservice method. It is throwing me an error as "The server cannot service the request because the media type is unsupported".
The code used to upload the image is as below :
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost("http://myWebServicelink/Service1.svc/UploadFile/");
ByteArrayBody bab = new ByteArrayBody(sendData,fileName);
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("f", bab);
reqEntity.addPart("fileName", new StringBody(fileName));
postRequest.setEntity(reqEntity);
HttpResponse response= httpClient.execute(postRequest);
HttpEntity entity = response.getEntity();
String result= EntityUtils.toString(entity);
The UploadFile method accepts two parameters as string UploadFile(byte[] f, string fileName);
and returns a string as "OK" if the image is transferred or throws an error if not.
The webservice method is as follows :
public string UploadFile(byte[] f, string fileName)
{
// the byte array argument contains the content of the file
// the string argument contains the name and extension
// of the file passed in the byte array
try
{
// instance a memory stream and pass the
// byte array to its constructor
MemoryStream ms = new MemoryStream(f);
// instance a filestream pointing to the
// storage folder, use the original file name
// to name the resulting file
FileStream fs = new FileStream
(System.Web.Hosting.HostingEnvironment.MapPath("~/TransientStorage/") +
fileName, FileMode.Create);
// write the memory stream containing the original
// file as a byte array to the filestream
ms.WriteTo(fs);
// clean up
ms.Close();
fs.Close();
fs.Dispose();
// return OK if we made it this far
return "OK";
}
catch (Exception ex)
{
// return the error message if the operation fails
return ex.Message.ToString();
}
}
Why is it throwing this error, i am able to figure out. What am i missing here ? Please help.