Android Question [Solved]Upload file with PostMultipart

samperizal

Active Member
Licensed User
Longtime User
Greetings.
I want to use a web api (Tested with postman and c#).
This has two parameters, a file and a token. The file does not reach the webapi. This is the code I am using.

[CODE lang="b4x" title="Code"] Dim URL As String = "https://a373-84-79-160-209.ngrok-free.app/Reconocimiento/Reconocer"
Dim j As HttpJob

j.Initialize("", Me)
Dim fd As MultipartFileData
fd.Initialize
fd.KeyName = "file"

fd.Dir = File.DirDefaultExternal
fd.FileName = "ejemplo.jpg"
fd.ContentType = "image/jpg"

j.PostMultipart(URL, CreateMap("Token": "1234"),Array(fd))

Wait For (j) JobDone(j As HttpJob)

If j.Success Then

Leer_Json_Respuesta_json (j.GetString)
Else
Log ("error")
Log(j.ErrorMessage)
Log(LastException)

End If
j.Release[/CODE]

Thank you.
 
ChatGPT suggestes try changing the line:
B4X:
fd.ContentType = "image/jpg"
'to
fd.ContentType = "image/jpeg"
 
Upvote 0
As a test, I would also try moving the source file from "File.DirDefaultExternal" to "File.DirInternal" just in case your app/device doesn't allow permission to access .DirDefaultExternal
 
Last edited:
Upvote 0
the file does not arrive
Well, I ran into a similar challenge :confused:. When you go to the web page https://a373-84-79-160-209.ngrok-free.app/Reconocimiento/Reconocer for the first time, you must first click on the "Visite site" button. When a cookie is placed in your browser with your visit time in it. How does your program ensure that it uses the cookie consent function "you first click on the Visit Site to go on" button ? If that doesn't happen, then (just like it was the case with me) it will be an endless wait.:eek:
 
Upvote 0
My workaround is not pretty, but it works by first visiting the site in web view, giving consent (the site places a cookie in the web view environment) and leaving out the consent cookie wall. So what you are looking for is a way to give permission with a B4A/B4J program so that the cookie is placed and the cookie wall is omitted.
 
Upvote 0
Greetings.
Still not uploading the file. I have made a routine in c# and it works correctly and the same in postman.
 
Upvote 0
Back
Top