1

I'm trying to send a JPG out of my app to a PHP listener and it seems like a ByteArray is the right way to handle this. However, when the request I currently have gets to the server, it has the fileName parameter (which is separate) but not the ByteArray param. In the case of the ByteArray it's missing both the key and value of the field.

In the debugger, I can see the data is in the request... perhaps I'm not collecting it correctly on the PHP side?

here are the relevant parts of my code... all help greatly appreciated (as well as advice to do this in some alternate, more effective, way).

ANDROID:

try {           
        Part[] parts = new Part[2];


        ByteArrayPartSource ps = new ByteArrayPartSource( printFileName, bitmapdata);
        parts[0] = new FilePart("fileData", ps);

        parts[1] = new StringPart("fileName" ,printFileName);

        filePost.setRequestEntity(new MultipartRequestEntity(parts,
                filePost.getParams()));

    } catch (Exception e) {
        Log.d("MY_DEBUG", e.toString());
    }

    try {
        int statusCode1 = client.executeMethod(filePost);

PHP:

$targetPath = "./" . $_REQUEST['fileName'] ; 


$newFilePath = $targetPath;

$fh = fopen($newFilePath, 'wb');    
    //tried both of these, neither seems to work... 
    fwrite($fh, $GLOBALS["HTTP_RAW_POST_DATA"]) or die("unable to write data");
            //and
    fwrite($fh, file_get_contents('php://input'));
    fclose($fh);

    echo "success!";
Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
  • hmm... I'm not sure what exactly I was doing wrong outside of the code posted above (maybe something on the PHP side) but this actually works perfectly as is. I'd like to delete this question. – Yevgeny Simkin Feb 23 '11 at 00:17
  • Check the link here i had posted a complete code to upload an image to a php server https://stackoverflow.com/questions/4896949/android-httpclient-file-upload-data-corruption-and-timeout-issues/4896988#4896988 http://getablogger.blogspot.com/2008/01/android-how-to-post-file-to-php-server.html It is a checked and tested thing Hope it helps :) – Rohit Sharma Feb 18 '11 at 09:51
  • thanks, I suppose I should just use my byteStream and call it a day. I've never had problems uploading files from Android before. I just decided to use a ByteArray, thinking it would be totally straight forward, but then spent 4 hours tearing hair out of my head as to why it's not working. – Yevgeny Simkin Feb 18 '11 at 10:03

0 Answers0