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 :)