0

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.

shivani
  • 746
  • 3
  • 22
  • 40
  • Maybe this post is helpfull:http://stackoverflow.com/questions/20798837/android-sending-image-to-server-via-multipartentity-setting-content-type – greenapps Aug 08 '14 at 10:57
  • Looks like you haven't set the content-type header. The server won't know unless you set that. – Lachlan Ennis Aug 08 '14 at 11:27
  • @LachlanEnnis i tried setting the content type header as `postRequest.setHeader("Content-Type:", "image/jpeg");` but this too was not yielding any response. What content type should i set?? – shivani Aug 08 '14 at 11:33
  • Check this. http://stackoverflow.com/questions/9404067/send-an-image-from-android-to-an-asp-net-web-service http://stackoverflow.com/questions/1617857/soapfault-exception-http-unsupported-media-type-when-accessing-java-web-servi – Lachlan Ennis Aug 08 '14 at 11:54

0 Answers0