php for receiving image and text from MultipartEntity

余生长醉 提交于 2020-01-13 07:14:13

问题


I'm trying to upload an image and some text via MultipartEntity. I can upload and receive the image, but when I try to add a Stringbody I cannot seem to receive it. Here's my android code

imports ETC...

public void oncreate(){
.....
nameValuePairs.add(new BasicNameValuePair("image", exsistingFileName));
nameValuePairs.add(new BasicNameValuePair("title", "title"));
}

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

    for(int index=0; index < nameValuePairs.size(); index++) {
        if(nameValuePairs.get(index).getName().equalsIgnoreCase("image")) {
            System.out.println("post - if");
            // 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 {
            System.out.println("post - else");
            // Normal string data
            entity.addPart(nameValuePairs.get(index).getName(), new StringBody(nameValuePairs.get(index).getValue()));
        }
    }
    System.out.println("post - done" + entity);

    httpPost.setEntity(entity);

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

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

And my php:

<?php

$uploads_dir = 'uploads/';

$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"]["title"];


move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>

I've been around the other questions about MultipartEntity, but cannot seem to find the answer. I've tried sending just the Stringbody, but didn't have any succs in that either. I think the problem is serverside (in the PHP) but any suggestions are welcome.

This is my first question in here - feel free to comment on form and clarity :-)


回答1:


try this way ,

         ByteArrayBody bab1 = bab11;
         HttpClient httpClient = new DefaultHttpClient();
         httpPost = new HttpPost("link.php?api_name=api");
         MultipartEntity reqEntity = new MultipartEntity(
                HttpMultipartMode.BROWSER_COMPATIBLE);
         // this is for String
      try {
            reqEntity.addPart("udid", new StringBody(UDID));
          } 
       catch (Exception e) 
          {
          }
              // this is for image upload 
          try 
          {
                reqEntity.addPart("file1", bab1);   
          } catch (Exception e) 
          {
          }
                 // this is for video upload 
      try {             
                if (stPath1 != null) {
                    Log.e("path 1", stPath1);
                    Log.v("stDocType1", "video");
                    File file = new File(stPath1);
                    FileBody bin = new FileBody(file);
                    reqEntity.addPart("file1", bin);
                }
        } catch (Exception e) {
        }

           httpPost.setEntity(reqEntity);
           ResponseHandler<String> responseHandler = new BasicResponseHandler();
           response = httpClient.execute(httpPost, responseHandler);



回答2:


The problem was i the php. When you receive Stringbody there is only one parametre(as opposed to filebody). So I removed the second parametre in $uploadtitle = $_FILES["title"]["title"]; and it worked

<?php
$uploads_dir = 'uploads/';

$uploadname = $_FILES["image"]["name"];
$uploadtitle = $_FILES["title"];


move_uploaded_file($_FILES['image']['tmp_name'], $uploads_dir.$uploadname);
file_put_contents($uploads_dir.'juhl.txt', print_r($uploadtitle, true));
?>

I hope this helps if you have the same problem.



来源:https://stackoverflow.com/questions/14380827/php-for-receiving-image-and-text-from-multipartentity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!