1

I am trying to upload an image file from an android app using java to an aspnet webapi with no success. I always get a response code of 500. Below is the basic code for preparing the sending of data from my android app. This code works when calling a php script but not when I call a web api controller shown below.

   FileInputStream fileInputStream = new FileInputStream(sourceFile);

           url = new URL("http://www.myserver.com/webapi/api/fileupload/post/thisfile");
           connection = (HttpURLConnection) url.openConnection();
            // Allow Inputs & Outputs.
            connection.setDoInput(true);
            connection.setDoOutput(true);
            connection.setUseCaches(false);
            // Set HTTP method to POST.
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Connection", "Keep-Alive");
            connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);
            outputStream = new DataOutputStream( connection.getOutputStream() );
            outputStream.writeBytes(twoHyphens + boundary + lineEnd);
            outputStream.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename=\"" + sourceFile +"\"" + lineEnd);
            outputStream.writeBytes(lineEnd);
            bytesAvailable = fileInputStream.available();
            bufferSize = Math.min(bytesAvailable, maxBufferSize);
            buffer = new byte[bufferSize];
            // Read file
            bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            while (bytesRead > 0)
            {
                outputStream.write(buffer, 0, bufferSize);
                bytesAvailable = fileInputStream.available();
                bufferSize = Math.min(bytesAvailable, maxBufferSize);
                bytesRead = fileInputStream.read(buffer, 0, bufferSize);
            }
            outputStream.writeBytes(lineEnd);
            outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
            // Responses from the server (code and message)
            serverResponseCode = connection.getResponseCode();

Below is the basic web api controller code to do the file upload:

 [HttpPost]
 [AcceptVerbs("GET", "POST")]
 public Task<HttpResponseMessage> Post()
    {
            // Check if the request contains multipart/form-data.
            if (!Request.Content.IsMimeMultipartContent())
            {
                //    return "Unsupported media type";
                throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
            }
            string root = "e:/web/myserver/htdocs/cemetery/pics/";
            var provider = new MultipartFormDataStreamProvider(root);

       return task;
    }

I changed the code above to send back a string and it says that my request does not contain multipart/form-data. Below is my webapiconfig.cs file route for this controller.

        config.Routes.MapHttpRoute(
           name: "FileUploadApi",
           routeTemplate: "api/{controller}/{action}/{name}", 
           defaults: new { action = "post", name = RouteParameter.Optional }
       );

I appreciate any help in determining what is wrong with my code so I can upload a file from an android java app to a webapi controller.

Dave
  • 873
  • 2
  • 15
  • 27
  • what is the connection.getResponseCode(); after opening the connection in first line? – Ravindra babu Sep 26 '15 at 15:59
  • The response code is 500 which doesn't tell you much. It throws an exception on the first instruction in the web api program: throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); – Dave Sep 26 '15 at 16:49
  • what is the file extension? Visit this article : http://androidexample.com/Upload_File_To_Server_-_Android_Example/index.php?view=article_discription&aid=83&aaid=106 – Ravindra babu Sep 26 '15 at 16:59
  • The file extension is .jpg. The example you referenced uses a php script to upload the file. I am already doing that with an existing php script. I want to be able to do the same thing with a wep api. There doesn't seem to be any examples out there for this scenario. – Dave Sep 26 '15 at 17:06
  • 'The response code is 500 which doesn't tell you much' ??? It's an http error code. What does it mean? You could have told that right away. – greenapps Sep 26 '15 at 17:19
  • I compared my java code and the code in the above example and the only difference I see is the url. The example references a php script while mine references a webapi controller. – Dave Sep 26 '15 at 17:20
  • I did say I received a 500 response code in my first paragraph. What I meant is that a 500 response code does not give you a specific reason. It means something is wrong but doesn't tell you what. – Dave Sep 26 '15 at 17:23
  • 'The example you referenced uses a php script to upload the file'. Don't think so. It will use a php script to receive the uploaded file. – greenapps Sep 26 '15 at 17:23
  • Please tell what http error 500 stands for. – greenapps Sep 26 '15 at 17:24
  • 1
    There is no specific error message for http error 500. It is a general error which you have to explore deeper on your own. – Dave Sep 26 '15 at 17:46
  • I understand that the example shows how to upload a file. My problem is receiving the file in webapi controller. I still don't see any difference in my java program and the example cited above. – Dave Sep 26 '15 at 17:48
  • 'There is no specific error message for http error 500. '. You are wrong. A little googling would tell you how it is named. – greenapps Sep 26 '15 at 21:03
  • If you know so much why don't you tell me what the 500 error is? You obviously aren't contributing anything to the solution. I am closing this question since it seems to be attracting argumentative trolls. – Dave Sep 27 '15 at 00:19
  • You can read [my answer here](http://stackoverflow.com/questions/16797468/how-to-send-a-multipart-form-data-post-in-android-with-volley/31718902#31718902) – BNK Sep 27 '15 at 04:46

0 Answers0