Android Question File Send With MultiPartPost and Httputils2

Marcos Alves

Well-Known Member
Licensed User
Longtime User
I searched in many threads but couldn't find how to do the sequence:

- POST a form with some token or even username and password, plus a file.
- IF token or username/password is valid, then the file (.csv) must to be written in a specific directory

I already tested cresting a html form and a php scrit those worked very well using browser, but didn't triggered the POST event when called using Job.PostMultipart ... the code that I used is:

Dim Job As HttpJob
Dim Param As Map
Dim Arqs As MultipartFileData
Arqs.Initialize
Param.Initialize
Job.Initialize("Contacts_Sent",Me)

Param.Put("username",Username)
Param.Put("password",Password)
Arqs.KeyName = "contacts"
Arqs.Dir = File.DirInternal
Arqs.ContentType = "text/csv"
Arqs.FileName = "contacts.csv"

Job.PostMultipart("https://myserver/uploadfile.html",Param,Array(Arqs))

In jobdone event I got only the html content of site - what means that the POST method wasn't sent ...

I noticed that I can use php://input and parse the raw data received but couldn't find also any parse script sample that is compatible with postmultipart encoding of b4a, meaning that this path will be very hard also... could anybody help me?
 

DonManfred

Expert
Licensed User
Longtime User
a html file can not do any saving for you. It is a static page.
You need to use a php script for ex. to get the post values and the upload an store the file like you expect.
A HTML can not do that.

See the link in my Signature for an Example (including the php-file).
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello DonManfred... thanks for quick answering! I understand that an html code can't manipulate the file but it can upload a tmp file that later can be accessed by the php script called in action/post method ... this is the html that I used:

<!DOCTYPE html>
<html>
<body>

<form action="upload.php" method="post" enctype="multipart/form-data">
<input type="text" name="username" id="username">
<input type="text" name="password" id="password">
<input type="file" name="contacts" id="contacts">
<input type="submit" value="Proceed" name="submit">
</form>

</body>
</html>

I noticed that when we choose the file, even before php script starts, the browser generates a upload stream sending that to a temporary file and directory managed by Apache server. When the button submit is pressed, a php script points to the temporary file, does the tests (file size, type and so on...) and move it to the definitive folder. This is the standard process. Unfortunately that process runs perfectly when called in browser, but not when using job.postmultipart method. I'm looking your posts but really would like to know why using browser the behavior in different than when using b4a method...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Ok... I'll use code tags - sorry, my fault.
The problem in posting directly to php is when we do this the post is received thru php://input stream and must to be parsed. Of course that I could analyse the stream and create my own parsing code but this is not the point. This MUST to be easier to be done... it's a trivial operation: receive two fields and a file... test two fields using php in a database... if ok, write definitive file. I'm downloading your programs and testing and will let this thread open to tell the results.
Thanks for your great help !
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello Manfred,

one more question: I noticed that you used the method httpclient instead of httpjob . In this post https://www.b4x.com/android/forum/threads/okhttp-replaces-the-http-library.54723/#content Erel used httpjob with the code:

B4X:
Dim j As HttpJob
j.Initialize("j", Me)
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "file"
fd.Dir = File.DirAssets
fd.FileName = "image.png"
fd.ContentType = "image/png"
j.PostMultipart("http://...", CreateMap("param1": "value1"), Array(fd))

Then I thought that the most recommended solution was to use httpjob ... so, what's the difference?
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Ok, thanks again. I'm testing your code and will reply soon. I'm a professional programmer/engineer and so doesn't matter if use http old, new... I need to deliver the solution working . Programs working quickly and perfectly means earning money ... the customer doesn't know the version of the internal modules.

I'll post the results here in one hour...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Thanks for help! Worked fine! Now, I'll implement my specific system details but the file transfer with additional parameters worked fine. I think that I already used your sample two years ago in one of my apps but the use of httpjob in Erel post confused me... I really thought that using directly httpjob was the more updated method.
I donated US$ 30 by your help. My lemma, helps me to earn money, earn money also.

Regards.
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Hello Manfred,
your help was very useful! One more thing: how can I do a postmultipart and receive back plain text and one or more files? I noticed that the http answer was a stream and so this can converted to file, text and so on.
But, how can I send text AND files back (php and B4A sides...). I think that the same upload code can be use with some small additions in server and client, but for me it's not clear now. Maybe you could have something ready...
 
Upvote 0

Marcos Alves

Well-Known Member
Licensed User
Longtime User
Ok, thanks again! I already used this technique using httpjob. I really didn't understand yet whats the difference between using job.postmultipart (httpjob) and the OkHttpClient with "hc.exe", but worked and that's what is important . Then, I'll use httpjob to download files and OkHttpClient to upload.
Hands on, I'm late.
Thanks my friend.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…