0

hello i am working with asp.net web API. i want to fetch a multi-part data from android app http post method. i can able to fetch file from request but i cant able to get string data associated with multi-part request.

here is my server code

 var fcount = HttpContext.Current.Request.Files.Count;
            for (i = 0; i <= fcount - 1; i++)
            {

                var files = HttpContext.Current.Request.Files[i];
                string FileName = Com.GetUnique(files.FileName);
                files.SaveAs(sPath + FileName);
            }

           // string root = HttpContext.Current.Server.MapPath("~/App_Data");
            var provider = new MultipartFormDataStreamProvider(sPath);


            try
            {
                await Request.Content.ReadAsMultipartAsync(provider);

                // Show all the key-value pairs.
                //foreach (var key in provider.FormData.AllKeys)
                //{
                    foreach (var val in provider.FormData.GetValues("data"))
                    {
                        string data = val;
                       // dynamic dat = js.Deserialize(data);
                        dynamic usr = js.DeserializeObject(data);

                        save.Db_data = fcount + "," + val;
                        db.Tempsave.Add(save);
                        db.SaveChanges();
                    }
              //  }

                return Request.CreateResponse(HttpStatusCode.OK);
            } 

here is the android code

 FileInputStream fileInputStream = new FileInputStream(sourceFile);
                    URL url = new URL(url1);

                    // Open a HTTP  connection to  the URL
                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true); // Allow Inputs
                    conn.setDoOutput(true); // Allow Outputs
                    conn.setUseCaches(false); // Don't use a Cached Copy
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("ENCTYPE", "multipart/form-data");
                    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary);

                    dos = new DataOutputStream(conn.getOutputStream());


                    dos.writeBytes(twoHyphens + boundary + lineEnd);


                    // JSON STRING
                    dos.writeBytes("Content-Disposition: form-data; name=\"data\"");

                    dos.writeBytes(lineEnd);

                    dos.writeBytes(json);
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + lineEnd);

                    // IMAGE
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"uploaded_file\";filename="+ sourceFileUri + "" + lineEnd);
                    dos.writeBytes(lineEnd);


                    // create a buffer of  maximum size
                    bytesAvailable = fileInputStream.available();

                    bufferSize = Math.min(bytesAvailable, maxBufferSize);
                    buffer = new byte[bufferSize];

                    // read file and write it into form...
                    bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                    while (bytesRead > 0) {

                        dos.write(buffer, 0, bufferSize);
                        bytesAvailable = fileInputStream.available();
                        bufferSize = Math.min(bytesAvailable, maxBufferSize);
                        bytesRead = fileInputStream.read(buffer, 0, bufferSize);

                    }

                    // send multipart form data necesssary after file data...
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);

                    // Responses from the server (code and message)
                    serverResponseCode = conn.getResponseCode();
                    String serverResponseMessage = conn.getResponseMessage();

how can i fetch the string associated with multi-part request.? please help thanks in advance :)

1 Answers1

0

Now as long as your form-data was written correctly, this should work. I had to make the same exact process work in a different situation but still using WebAPI. Let me know if it works and if it doesn't, I will try and assist further.

foreach (var val in provider.Contents)
{
    switch (val.Headers.ContentDisposition.Name)
    {
        case "data":
            string name = val.ReadAsStringAsync().Result;
            break;
    }
}

EDIT: also.. that switch statement can easily be:

if (val.Headers.ContentDisposition.Name == "data") string name = val.ReadAsStringAsync().Result;
terbubbs
  • 1,512
  • 2
  • 25
  • 48
  • it is not entering if loop – Manu prasad Dec 09 '15 at 16:41
  • @Manuprasad just to make sure, you are not having any issues retrieving the Image you are posting, correct? – terbubbs Dec 09 '15 at 17:49
  • @Manuprasad when you iterate through the foreach loop, what are the values of 'val'? – terbubbs Dec 09 '15 at 17:59
  • hi its working fine with fiddler in localhost and server, but from android side only getting the image not string data. is it problem of android ? please help me. – Manu prasad Dec 10 '15 at 05:07
  • @Manuprasad then make sure your JSON string is correct. take a look at this: http://stackoverflow.com/questions/31351676/building-proper-json-object-in-android-for-an-asp-net-web-service – terbubbs Dec 10 '15 at 13:14
  • @Manuprasad try building the JSON object like that and using the OutputStream that the correct answer provides. if Fiddler is working fine for you, then it's your android code and not the receiving end. – terbubbs Dec 10 '15 at 13:16