4

I've been looking all day about how to upload image from my application to server side. I've done the mobile application side

public static void postImage(String ImageLink){
    RequestParams params = new RequestParams();
    params.put("uploaded_file[name]", "MyImageName");
    try {
        params.put("uploaded_file[image]", new File(ImageLink));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    AsyncHttpClient client = new AsyncHttpClient();
    client.post(MY_PHP_FILE_LINK, params, new AsyncHttpResponseHandler() {

        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            System.out.println("statusCode "+statusCode);//statusCode 200
        }

        @Override
        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

        }
    });
}

and my php side code

<?php

$file_path = "/uploads/";

$file_path = $file_path . basename( $_FILES['uploaded_file']['name']);
if(move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $file_path)) {
    echo "success";
} else{
    echo "fail";
}?>

but I cant find the image on server even the statusCode was OK

what is the problem?? is my php code wrong?

5
  • show your body plz of your response Commented Jul 29, 2015 at 11:37
  • @eddykordo it only show fail Commented Jul 29, 2015 at 11:43
  • maybe you have wrong chmod for your uploads folder Commented Jul 29, 2015 at 11:52
  • @eddykordo it set to 775 I've used another method for uploading the image using standard http request and it works, but I want to use the post method of android-sync-http library because I use it in my whole application and its very good Commented Jul 29, 2015 at 11:56
  • @eddykordo I've fixed the problem :) Commented Jul 29, 2015 at 12:31

1 Answer 1

10

I've Solved the problem, it was in my RequestParams I've tried to change the file name but it seems I can't do it so that I've change my code and it works fine

public static void postImage(String ImageLink){
RequestParams params = new RequestParams();
try {
    params.put("uploaded_file", new File(ImageLink));
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
AsyncHttpClient client = new AsyncHttpClient();
client.post(MY_PHP_FILE_LINK, params, new AsyncHttpResponseHandler() {

    @Override
    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
        System.out.println("statusCode "+statusCode);//statusCode 200
    }

    @Override
    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

    }
});
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you! I have problem with loading image: 413 Request Entity Too Large. Search solve again :)

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.