Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android-async-http uploading image to php server

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?

like image 659
Khalil Rumman Avatar asked Dec 24 '22 16:12

Khalil Rumman


1 Answers

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) {

    }
});
}
like image 101
Khalil Rumman Avatar answered Dec 27 '22 20:12

Khalil Rumman