-3

In my application i have to send the csv file to server i tried the following code

            HttpPost httppost = new HttpPost(url);

        InputStreamEntity reqEntity = new InputStreamEntity(
                new FileInputStream(file), -1);
        reqEntity.setContentType("binary/octet-stream");
        reqEntity.setChunked(true); // Send in multiple parts if needed
        httppost.setEntity(reqEntity);
        HttpResponse response = httpclient.execute(httppost);

and my php code is..

<?php


if ($_FILES["detection"]["error"] > 0)
{
echo "Return Code: " . $_FILES["detection"]["error"] . "<br>";
 }

else {

 if (file_exists($_FILES["detection"]["name"]))
  {
   echo $_FILES["detection"]["name"] . " already exists. ";
  }
 else
   {
  move_uploaded_file($_FILES["detection"]["tmp_name"],$_FILES["detection"]["name"]);
  echo "Stored in: ". $_FILES["detection"]["name"];
   }
 }

?>

i got the error that

08-26 17:29:18.318: I/edit user profile(700):
08-26 17:29:18.318: I/edit user profile(700): Notice: Undefined index: detection in C:\xampp\htdocs\sendreport.php on line 4

1 Answer 1

4

I hope it will work

 // the file to be posted
String textFile = Environment.getExternalStorageDirectory() + "/sample.txt";
 Log.v(TAG, "textFile: " + textFile);

 // the URL where the file will be posted
 String postReceiverUrl = "http://yourdomain.com/post_data_receiver.php";
Log.v(TAG, "postURL: " + postReceiverUrl);

 // new HttpClient
 HttpClient httpClient = new DefaultHttpClient();

// post header
HttpPost httpPost = new HttpPost(postReceiverUrl);

File file = new File(textFile);
FileBody fileBody = new FileBody(file);

MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
reqEntity.addPart("file", fileBody);
httpPost.setEntity(reqEntity);

// execute HTTP post request
HttpResponse response = httpClient.execute(httpPost);
HttpEntity resEntity = response.getEntity();

if (resEntity != null) {

String responseStr = EntityUtils.toString(resEntity).trim();
Log.v(TAG, "Response: " +  responseStr);

// you can add an if statement here and do other actions based on the response
}

and php code.
<?php
// if text data was posted
if($_POST){
print_r($_POST);
}

 // if a file was posted
 else if($_FILES){
 $file = $_FILES['file'];
 $fileContents = file_get_contents($file["tmp_name"]);
 print_r($fileContents);
 }
 ?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.