Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T05:58:59+00:00 2026-06-07T05:58:59+00:00

I have this form for uploading images to the server using HttpPost. I did

  • 0

I have this form for uploading images to the server using HttpPost. I did it using a HTML form but it doesn’t work with Android’s HttpPost. It responses: “You did not select a file to upload”. It seems that file field hasn’t been sent.

<html>
<head>
<title>Upload Form</title>
</head>
<body>


<form action="http://192.168.0.151/index.php/upload/uploadFile" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<input type="file" name="userfile" size="20" />

<br /><br />

<input type="submit" value="upload" />

</form>

</body>
</html>

Client code

    final HttpClient client = new DefaultHttpClient();
    final HttpPost post = new HttpPost("http://" + hostName + "/upload/uploadFile");
    post.addHeader("enctype", "multipart/form-data");

    final List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("userfile", "/mnt/sdcard/Download/Photos/icecream.png"));

    try {
        final HttpEntity request = new UrlEncodedFormEntity(nameValuePairs);            
        post.setEntity(request);            

        final HttpResponse response = client.execute(post);

        // Get response body.
        final String responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("RESPONSE BODY: " + responseBody);

    } catch (final UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (final ClientProtocolException e) {
        e.printStackTrace();
    } catch (final IOException e) {
        e.printStackTrace();
    }

Controller class

class Upload extends CI_Controller {

    function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', 'url'));
    }

    public function showForm() {
        $this->load->view('upload_form');
    }

    public function uploadFile() {
        // Header for xml outputing.
        header('Content-type: text/xml; charset=utf-8');

        $config['upload_path'] = './uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '2048';
        $config['max_width']  = '2048';
        $config['max_height']  = '2048';

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload())
        {
            // Get error message.
            $error = $this->upload->display_errors();

            // Prepare template.
            $xmlData = file_get_contents(TEMPLATE_XML_DIR . "upload_result.xml");
            $xmlData = str_replace("{IS_SUCCESSFUL}", 0, $xmlData);
            $xmlData = str_replace("{ERROR}", $error, $xmlData);
            echo $xmlData;
        }
        else
        {
            $data = array('upload_data' => $this->upload->data());
            // Prepare template.
            $xmlData = file_get_contents(TEMPLATE_XML_DIR . "upload_result.xml");
            $xmlData = str_replace("{IS_SUCCESSFUL}", 1, $xmlData);
            $xmlData = str_replace("{ERROR}", "", $xmlData);
            echo $xmlData;
        }
    }
}

……………..

Update solution (it works for my code). Remember to add “httpmime-4.2.1.jar” to your Build Path.

public void post(final String url, final List<NameValuePair> nameValuePairs) {
    final HttpClient httpClient = new DefaultHttpClient();
    final HttpContext localContext = new BasicHttpContext();
    final HttpPost httpPost = new HttpPost(url);

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

        for(int index=0; index < nameValuePairs.size(); index++) {
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("userfile")) {
                // If the key equals to "userfile", we use FileBody to transfer the data
                entity.addPart(nameValuePairs.get(index).getName(), new FileBody(new File (nameValuePairs.get(index).getValue())));
            } else {
                // Normal string data
                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
            }
        }

        httpPost.setEntity(entity);

        final HttpResponse response = httpClient.execute(httpPost, localContext);

        final String responseBody = EntityUtils.toString(response.getEntity());
        System.out.println("RESPONSE BODY: " + responseBody);
    } catch (final IOException e) {
        e.printStackTrace();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T05:59:01+00:00Added an answer on June 7, 2026 at 5:59 am

    I just ran into this answer relating to image posting. Does this help?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code for uploading files on the server: <tr> <td> <form enctype=multipart/form-data
I have to upload images to server form iphone I am using ASIHTTPRequest for
I have this Form I am trying to upload my image but every time
I need to upload a pic on server without using html form tag. Is
I am uploading large files to an ASP.NET server using a standard HTML <input>
I have got this JavaScript code for uploading files to my server (named it
I have a form for uploading images. I'd like to disable the submit button,
I have a form with flask-wtf for uploading images, also file field can be
I have this form, in which i need to populate a combo box with
I have this form: <%= form_tag posts_path, :method => :get, :class => search_nav do

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.