up vote 0 down vote favorite
1
share [fb]

I have the following code to send a file (mpeg file - about 20kb) from the phone to the server. However, it fails at the server end. Can anyone kindly telly me what mistake I am making at the client end ? Thanks.

ByteArrayOutputStream bos = new ByteArrayOutputStream();
        //bm.compress(CompressFormat.JPEG, 75, bos);
        byte[] data = bos.toByteArray();
        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://example.com/upload.php");
        File file= new File("/mnt/sdcard/enca/aha.mpeg");
        FileBody bin = new FileBody(file);
        MultipartEntity reqEntity = new     MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        reqEntity.addPart("uploaded", bin);
        reqEntity.addPart("random", new StringBody(encameo1.random));
        reqEntity.addPart("fingerPrint", new StringBody(encameo1.fingerprint));
        postRequest.setEntity(reqEntity);
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();

        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
        System.out.println("Response: " + s);

php code:

<?php



$target_path = "uploaded_files/";

$target_path = $target_path . basename( $_FILES['userfile']['name']); 

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) 
{

    echo "The file ".  basename( $_FILES['userfile']['name'])." has been uploaded";

} 

else
{

    echo "There was an error uploading the file, please try again!";

}



?>
link|improve this question

67% accept rate
feedback

1 Answer

up vote 3 down vote accepted

You're sending the part with the name uploaded:

reqEntity.addPart("uploaded", bin);

However, PHP is expecting a part with the name userfile:

if(move_uploaded_file($_FILES['userfile']['tmp_name'], $target_path)) 

Align it to be the same.

link|improve this answer
was porting iOS app to android and didnt realize the mistake. thanks a bunch. – Ahsan Sep 27 at 4:53
Can happen. You're welcome. – BalusC Sep 27 at 4:53
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.