开发者

Android Image Upload Fails

I'm trying to upload an image from my phone to a server. So I get the image uri and create a NameValuePair with it. I then use the following Android code to get a connection and upload the pic. This was taken from previous stackoverflow question(I would link but can't find it even with Google...)

public void post(String url, List<NameValuePair> nameValuePairs) {

    HttpClient httpClient = new DefaultHttpClient();
    HttpContext localContext = new BasicHttpContext();
    HttpPost httpPost = new HttpPost(url);

    try {
           MultipartEntity entity = new  
                       MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);


             entity.addPart(nameValuePairs.get(0).getName(), new FileBody(new File(nameValuePairs.get(0).getValue())));
        Log.v("Uploading file",nameValuePairs.get(0).getValue());
        httpPost.setEntity(entity);
            HttpResponse response = httpClient.execute(httpPost);
           Log.v("Response", response.toString());
        } catch (IOException e) {
          e.printStackTrace();
        }
   }

On the server side I have the following code. This is PHP, which I am a complete novice at. I know there is no validation in the code, but I want to get the upload working before I start worrying about everything else.

      <?php

     if($_FILES){
           $name = $_FILES['image']['name'];
           move_uploaded_file($_FILES['image']['temp'],$name);

           echo "success!";
     }
    else {

  echo "Nothing uploaded";
   }
 ?>

Here the name of the NameValuePair is 'image', which is used as the file name.

Nothing gets uploaded, but a connection is made. Please help.

Following the开发者_开发技巧 request I put print_r($Files) in the code. The respons from the server was:

09-05 23:23:35.745: VERBOSE/Response(13849): Array

09-05 23:23:35.745: VERBOSE/Response(13849): (

09-05 23:23:35.745: VERBOSE/Response(13849):     [image] => Array

09-05 23:23:35.745: VERBOSE/Response(13849):         (

09-05 23:23:35.745: VERBOSE/Response(13849):             [name] => DSC_0100.jpg

09-05 23:23:35.745: VERBOSE/Response(13849):             [type] => 

09-05 23:23:35.745: VERBOSE/Response(13849):             [tmp_name] => /tmp/phpXcY8L1

09-05 23:23:35.745: VERBOSE/Response(13849):             [error] => 0


09-05 23:23:35.745: VERBOSE/Response(13849):             [size] => 806873

09-05 23:23:35.745: VERBOSE/Response(13849):         ) 09-05 23:23:35.745: VERBOSE/Response(13849): )

09-05 23:23:35.745: VERBOSE/Response(13849):**


move_uploaded_file($_FILES['image']['temp'],$name); replace this line with

move_uploaded_file($_FILES['image']['tmp_name'],$name);

The file's temporary file name is in "tmp_name", as i can see from your log output.


On server side please add this code ,

print_r($_FILES);

then check for array parameter 'error' then check error code from here http://php.net/manual/en/features.file-upload.errors.php

if you want the file upload code for android then please tell me i can post here

i hope this will help you.

Thank you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜