0

hello i need when i click to button "send" i send all selected picture ( i have all this picture in table picture[]

in PHP i do this

<input id="uploadImageAct"  type="file" name="uploadImageAct[]"  data-max-size="2048"  accept="image/x-png, image/gif, image/jpeg" style="visibility: hidden" multiple="multiple">

and in android for just one picture i do this but i don't know how i do for multi picture (I put all my picture in table picture[] )

this solution is for one image for FileBody and me i need multi image in one FileBody

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

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

        for(int index=0; index < nameValuePairs.size(); index++) {
            if(nameValuePairs.get(index).getName().equalsIgnoreCase("uploadFile")) {
                // If the key equals to "image", 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
             Charset chars = Charset.forName("UTF-8");
                entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue(),chars));
            }
        }

        httpPost.setEntity(entity);

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


    HttpEntity httpEntity = response.getEntity();
    is = httpEntity.getContent();

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

try {
    BufferedReader reader = new BufferedReader(new InputStreamReader(
            is, "iso-8859-1"), 8);
    StringBuilder sb = new StringBuilder();
    String line = null;
    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    is.close();
    json = sb.toString();
} catch (Exception e) {
    Log.e("Buffer Error", "Error converting result " + e.toString());
}

// try parse the string to a JSON object
try {
    jObj = new JSONObject(json);
} catch (JSONException e) {
    Log.e("JSON Parser", "Error parsing data " + e.toString());
}

// return JSON String
return jObj;
}

How can I change this function so that I can do this

thanks :)

slama007
  • 1,273
  • 2
  • 18
  • 34

1 Answers1

1

You can upload multiple files in one request along with other string parameters in Android. For that, you have to include 2 libraries into your project build path, apache-mime4j-0.6.jar and httpmime-4.0.1.jar.

private void doFileUpload(){

    File file1 = new File(selectedPath1);
    File file2 = new File(selectedPath2);
    String urlString = "Your server location";
    try
    {
         HttpClient client = new DefaultHttpClient();
         HttpPost post = new HttpPost(urlString);
         FileBody bin1 = new FileBody(file1);
         FileBody bin2 = new FileBody(file2);
         MultipartEntity reqEntity = new MultipartEntity();
         reqEntity.addPart("uploadedfile1", bin1);
         reqEntity.addPart("uploadedfile2", bin2);
         reqEntity.addPart("user", new StringBody("User"));
         post.setEntity(reqEntity);
         HttpResponse response = client.execute(post);
         resEntity = response.getEntity();
         final String response_str = EntityUtils.toString(resEntity);
         if (resEntity != null) {
             Log.i("RESPONSE",response_str);
             runOnUiThread(new Runnable(){
                    public void run() {
                         try {
                            res.setTextColor(Color.GREEN);
                            res.setText("n Response from server : n " + response_str);
                            Toast.makeText(getApplicationContext(),"Upload Complete. Check the server uploads directory.", Toast.LENGTH_LONG).show();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                       }
                });
         }
    }

Or simply visit CoderzHeaven, it may help you.

Pang
  • 9,564
  • 146
  • 81
  • 122
Shailesh
  • 442
  • 7
  • 14
  • this solution is for one image for FileBody and me i need multi image in one FileBody – slama007 Feb 13 '14 at 11:09
  • But the meaning is same, You want multiple images in one FileBody, for that you have to send one request to server and I had solution with multi images in multi FileBody but with same one request... if Still you go for yours then I suggest, ArrayList. take all images in arraylist, and send it to server. – Shailesh Feb 13 '14 at 12:17