Android Question Help with multipart uploads

kostefar

Active Member
Licensed User
Longtime User
Dear Community,

I´ve been through all that I could find about this subject, yet I did not find an answer to my question:

Assuming that multipart uploads are used for sending chunks of a file one by one to a server, where do I set the size of each chunk in the b4a code?

In my case, the host I use allows me only to post URLs with a max size of 8192 bytes, and since Android seems not to be your friend when it comes to setting up a server on the device, from which the host can retrieve files, multipart uploads seem to be the only option I have.

Thanks in advance!
 

DonManfred

Expert
Licensed User
Longtime User
I guess you are confusing things....

If you are talking about URLs of a max size of 8kb.
you are uploading a file (as data in the url? You are doing a GET-Request?

You really should have posted your code. Without the code we just can guess what you did....


A Multipartupload is this (but these are not chunks. The complete file is uploaded)

Use okhttputils2

B4X:
Sub btnuplimg_Click
    '  
    ' Upload von Files für ein Projekt....
    '
    Dim files As List
    files.Initialize
  
    ' File 1. Ein jpg
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "imagejpg"
    fd.Dir = File.DirAssets
    fd.FileName = "20141014_175713.jpg"
    fd.ContentType = "image/jpg"
    files.Add(fd)

    ' File 2. Ein png
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "imagepng"
    fd.Dir = File.DirAssets
    fd.FileName = "DonManfred.png"
    fd.ContentType = "image/png"
    files.Add(fd)

    ' File 3. Ein pdf
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = "pdf"
    fd.Dir = File.DirAssets
    fd.FileName = "Ssykor101006-01PCMannes.pdf"
    fd.ContentType = "application/pdf"
    files.Add(fd)


    Dim job As HttpJob
    job.Initialize("Upload",Me)
    job.PostMultipart("http://domain.com/",CreateMap("action":"upload"),files)
 
Last edited:
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
I guess you are confusing things....

If you are talking about URLs of a max size of 8kb.
you are uploading a file (as data in the url? You are doing a GET-Request?

You really should have posted your code. Without the code we just can guess what you did....

Thanks DonManfred,

Not much code to post as I do not know how to split the file but was advised elsewhere here to use multipart upload as a workaround to the limit that my host has got. This is regardless of whether if I use POST or GET, and the host has confirmed this being a limit on the cheap hosting plan that I have.
I simply receive an error from their nginx server as soon as the URL size exceeds 8192 bytes. I tried to see if it´s before it reaches the PHP engine by misspelling my URL by mistake and entering it into a browser so for instance .../send_dat0.php***8193bytesofdata instead of for instance .../send_data.php***8193bytesofdata and the error still pops up.
 
Upvote 0

kostefar

Active Member
Licensed User
Longtime User
Post a sample of your code which do the upload.

Without seeing what you did and how it is hard to give advices.

Ok, you´re right. Now this is what I´d do in the past because that´s the only way I could get it to work:

B4X:
postmessagejob.poststring  (MainURL &  "post_message.php?" & parametervalues,"")

Which would always work - up till the before mentioned size limit on server side.

But right now I just started to think that perhaps if I did it in the recommended way:

B4X:
postmessagejob.poststring  (MainURL &  "post_message.php" ,parametervalues)

.. I´m one step closer to something that´ll work.
But not there yet. I just tried this, both with and without "?" after .php, and I can see that nothing is happening to the mysql table where data should be inserted as they would with the first method mentioned. No error code given either.
Hence, I checked in my network monitor, and this is what I see in the first packet:

B4X:
POST /php/post_message.php HTTP/1.1..Content-Length: 265..Content-Type: application/x-www-form-urlencoded..Host: www(removed).com..Connection: Keep-Alive....

And then a new packet arrives with the rest:

B4X:
usernametmp=xx&passwordtmp=xx&jsonarray=[%7B%22timestamp%22:%222017-02-02%2010:53:29%22,%22sender%22:%22yy%22,%22message%22:%22jiojjojo%22,%22attachment%22:null,%22threadid%22:%2292204805202022017235329%22,%22msgtype%22:%220%22,%22subject%22:%22%22%7D]

So when using this method, the server does not seem to understand that the next packet contains data which is part of the first packet.

Do you think I´m right that this would be working, and do you know if there´s a way to construct the posted data so that the server will know to "glue" the packets?

EDIT: Ok, so after playing around with this for a bit, I found some other threads telling that when using the method where URL and data is separated, you´ll have to use php://input on the server side. Hence, I checked my script and now I have modified it to include two different ways of displaying the input - just in case:

B4X:
$json = file_get_contents("php://input");
echo $json;
$fp = fopen("php://input", 'r+');
while (!feof($fp)) {
  $contents .= fread($fp, 8192);
}
fclose($fp);
echo $contents;

But nothing shows up, and I´ve already checked in php.ini that I have allow_url_fopen = On.

EDIT AGAIN:

I think it´s resolved now! always_populate_raw_post_data = On had to be set in php.ini. Gonna check if big messages can get through now and let you know.


Thanks!

Final EDIT: I went for your multipart solution and found that it did the job perfectly!
 
Last edited:
Upvote 0
Top