Thursday, April 11, 2013

[android help] Invalid file on uploading text file to sever with HttpPost?


I'm trying to upload a text file that already is in SD-card of emulator and it's size is less than 4KB.This is php code for uploading file in server side:



$allowedExts = array("jpg", "jpeg", "gif", "png", "txt", "xml");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "text/plain")
|| ($_FILES["file"]["type"] == "text/xml"))
&& ($_FILES["file"]["size"] < 2000000)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "
";
}
else
{
echo "Upload: " . $_FILES["file"]["name"] . "
";
echo "Type: " . $_FILES["file"]["type"] . "
";
echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB
";
echo "Temp file: " . $_FILES["file"]["tmp_name"] . "
";

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
}
}
}
else
{
echo "Invalid file";
}
?>


To upload text file,I execute an AsyncTask and it is it's doInBackground():



@Override
protected Boolean doInBackground(String... url) {
File home = new File(path + "/" + "home #" + address + ".txt");
String response;
try {
response = OpenHttpConnection(home, "text/plain");
} catch (IOException e) {
return false;
}
if (response.toLowerCase().contains("invalid")) {
return false;
}
return true;
}


This is OpenHttpConnection method:



private String OpenHttpConnection(File file, String mime)
throws IOException {

StringBuilder s = null;
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
Constants.UPLOAD_ADDRESS);

MultipartEntity entity = new MultipartEntity();

entity.addPart("data", new FileBody(file, mime));
httppost.setEntity(entity);
HttpResponse response = httpclient.execute(httppost);
BufferedReader reader = new BufferedReader(new InputStreamReader(
response.getEntity().getContent(), "UTF-8"));
String sResponse;
s = new StringBuilder();

while ((sResponse = reader.readLine()) != null) {
s = s.append(sResponse);
}

} catch (Exception e) {
e.printStackTrace();
return "exception " + e.toString();
}

return s.toString();
}


But upload failed and when I debug uploading progress,response(in "doInBackground") is equal to "Invalid file".Also I tried to change OpenHttpConnection() method and create entity of HttpPost without using "mime type",like this:



entity.addPart("data", new FileBody(file));


But result is same.Is there any thing that I did wrong?



.

stackoverflow.comm

No comments:

Post a Comment

Google Voice on T-Mobile? [General]

Google Voice on T-Mobile? So I recently switched from a GNex on Verizon to a Moto X DE on T-Mobile. I had always used Google Voice for my v...