Android Question B4J Tutorial - [Server] Upload files from your B4A app to your B4J server over the internet

anz coputer solutions

Member
Licensed User
Longtime User
A year ago, I had no problem changing the file extensions manually, but now I receive more than 70 files a day and it's hard to keep track of all please advise.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You never need to use PHP if you are using B4J.

The file name is already passed in this example. Check the server logs. It should print the name:
B4X:
Case "file"
   Dim name As String = req.GetParameter("name")
   Dim out As OutputStream = File.OpenOutput(Main.filesFolder, name, False)
   File.Copy2(In, out)
   out.Close
   Log("Received file: " & name & ", size=" & File.Size(Main.filesFolder, name))
   resp.Write("File received successfully.")
 
Upvote 0

anz coputer solutions

Member
Licensed User
Longtime User
this is what I getting on the log.
 

Attachments

  • Capture.PNG
    Capture.PNG
    21.5 KB · Views: 190
Upvote 0

anz coputer solutions

Member
Licensed User
Longtime User
#Region Project Attributes


#ApplicationLabel:Uploader

#VersionCode: 1

#VersionName:

'SupportedOrientations possible values: unspecified, landscape or portrait.

#SupportedOrientations: portrait

#CanInstallToExternalStorage: False

#End Region



#Region Activity Attributes

#FullScreen: False

#IncludeTitle: False

#End Region



Sub Process_Globals

Private link As String = "http://duckdns.org:54021/upload"

Private cc As ContentChooser

End Sub



Sub Globals

End Sub



Sub Activity_Create(FirstTime As Boolean)

If FirstTime Then

cc.Initialize("cc")

End If

Activity.LoadLayout("1")

End Sub



Sub btnSendFile_Click

cc.Show("*/*", "Choose file")

End Sub



Sub cc_Result (Success As Boolean, Dir As String, FileName As String)

If Success Then

Dim j As HttpJob

Dim out As OutputStream

out.InitializeToBytesArray(0)

Dim In As InputStream = File.OpenInput(Dir, FileName)

File.Copy2(In, out)



Dim lastSlash As Int = FileName.LastIndexOf("/")

If lastSlash > -1 Then

FileName = FileName.SubString(lastSlash + 1)

End If

Dim su As StringUtils

Dim j As HttpJob

j.Initialize("file", Me)

j.PostBytes(link & "?type=file&name=" & su.EncodeUrl(FileName, "UTF8"), _

out.ToBytesArray)

End If

End Sub



Sub JobDone(j As HttpJob)

If j.Success Then

ToastMessageShow("Success: " & j.GetString, False)

Log("Success: " & j.GetString)

Else

ToastMessageShow("Error: " & j.ErrorMessage, True)

Log("Error: " & j.ErrorMessage)

End If

j.Release

End Sub



Sub Activity_Resume



End Sub



Sub Activity_Pause (UserClosed As Boolean)



End Sub
 
Upvote 0

anz coputer solutions

Member
Licensed User
Longtime User
sorry I don't understand your question on the log say Success: File received successfully.
this is the firs time the I'm asking for help and I can get it I'll wait for 4 days
and nothing.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi ,
browsing your code I see
B4X:
 j.PostBytes(link & "?type=file&name=" & su.EncodeUrl(FileName, "UTF8"), out.ToBytesArray)
that shows how the file name is transmitted once it is (correctly) encoded by EncodeURL function.
On the receiving end, I don't know if
B4X:
 Dim name As String = req.GetParameter("name")
will decode it for you, but you could try it yourself using su.DecodeURL(name, "UTF8") to see whether it gives you back the file name in the form you need to create the output file.

Sorry that I couldn't try before posting, but I'm not at my dev PC now.
 
Upvote 0

anz coputer solutions

Member
Licensed User
Longtime User
<Logger connected to: Google Pixel XL

--------- beginning of main

** Activity (main) Create, isFirst = true **

** Activity (main) Resume **

** Activity (main) Pause, UserClosed = false **

sending message to waiting queue (OnActivityResult)

running waiting messages (1)

FileName: content://com.android.providers.media.documents/document/image%3A9964

** Activity (main) Resume **

*** Service (httputils2service) Create ***

** Service (httputils2service) Start **

Success: File received successfully.

** Activity (main) Pause, UserClosed = false **>
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Maybe this could be of some help.
 
Upvote 0
Top