Android Question Post multipart

jayel

Active Member
Licensed User
Longtime User
hello,

I want to send jpg files to my server.
I want to use postmultipart, i do it like this :
B4X:
Dim fd As MultipartFileData
    fd.Initialize
    fd.Dir = Starter.imageFolder ' the folder where the images are stored
    Dim mydetail As orderdetail = listorderdetails.Get(0)
    fd.FileName = mydetail.afbeelding1 'the location of the file
    fd.keyname = "foto"
    fd.ContentType = "image/jpeg"
   
    files.Add(fd)
 
    Dim job2 As HttpJob
    job2.Initialize("Overzetten_afbeeldingen", Me)
    job2.PostMultipart(Starter.URLstring & "multipartpost.php", Null,files)
My php :
PHP:
<?php
header("Content-type: text/plain");




    $file_handle = fopen('./multipartpost.log', 'a+');
    fwrite($file_handle, "======================================"."\r\n");
    foreach($_REQUEST as $name => $value){
        fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$name."=".$value."\r\n");
    }
    fwrite($file_handle, "======================================"."\r\n");
   
    $uploads[] = array();
    fwrite($file_handle, "======= FILES ========================"."\r\n");
    foreach($_FILES as $name => $value){
        $uploads[$name] = $value;
        foreach($value as $fname => $fvalue){
            fwrite($file_handle, date("d.m.Y H:i:s", time()).": ".$fname."=".$fvalue."\r\n");
        }
        fwrite($file_handle, date("d.m.Y H:i:s", time()).": Upload of \"".$name."\"\r\n");
        if($name=="foto"){
            $uploaddir = './images/';
            $uploadfile = $uploaddir . basename($_FILES[$name]['name']);
            fwrite($file_handle, date("d.m.Y H:i:s", time()).": MoveUploadedFile(".$_FILES[$name]['name'].")\r\n");
            if (move_uploaded_file($_FILES[$name]['tmp_name'], $uploadfile)) {
                $uploads[$name]["status"] = $_FILES[$name]['name']." saved successfull";
                fwrite($file_handle, date("d.m.Y H:i:s", time()).": ->moving ".$_FILES[$name]['name']." successfull\r\n");
            #echo "Datei ist valide und wurde erfolgreich hochgeladen.\n";
            }
        }           
        else {
            $uploads[$name]["status"] = $_FILES[$name]['name']." failed to save!";
            fwrite($file_handle, date("d.m.Y H:i:s", time()).":->moving ".$_FILES[$name]['name']." NOT successfull\r\n");
            #echo "Möglicherweise eine Dateiupload-Attacke!\n";
        }
    

    }
    fwrite($file_handle, "======================================"."\r\n");
    #print_r($uploads);
    $appresult["uploads"] = $uploads;

    echo json_encode($appresult);
    #

    fclose($file_handle);
?>

That is a copy of donmanfred's code.

But on my server I got this in the multipart :
======= FILES ========================
10.11.2016 16:05:32: name=1478597511098_foto_0.jpg
10.11.2016 16:05:32: type=
10.11.2016 16:05:32: tmp_name=
10.11.2016 16:05:32: error=1
10.11.2016 16:05:32: size=0
10.11.2016 16:05:32: Upload of "foto"
10.11.2016 16:05:32: MoveUploadedFile(1478597511098_foto_0.jpg)
======================================

I have to say that I use a slightly modified version of httpJob, because I use https.
in attachtment the file.

I really have no clue what to debug next. When I look at the log file it seems the size is 0?
Is it because my jpg files are to big?

greets John
 

Attachments

  • HttpJob.bas
    6 KB · Views: 262

DonManfred

Expert
Licensed User
Longtime User
fd.Filename should be the FILENAME not a path.
B4X:
fd.Dir = File.DirAssets
fd.FileName = "filename.jpg"
    fd.ContentType = "image/jpeg"
 
Upvote 0

jayel

Active Member
Licensed User
Longtime User
fd.Filename should be the FILENAME not a path.
B4X:
fd.filename = "filename.jpg"

Yes, thats right a typo, it is the filename in my example it is :
1478854045122_foto_0.jpg
As you can see in my spoiler in the first post.

Strange that the file size is 0

What could it be?
 
Upvote 0

jayel

Active Member
Licensed User
Longtime User
ok, where getting somewhere, I will look on my local webserver if I can change the size becaus a small png file sends succesfully....
 
Upvote 0
Top