B4J Question Simple FTP download

fabio55

Member
Licensed User
Longtime User
Thanks Erel, but what about the parameters (port, user, password)? And I need a string not a file. Should I read the downloaded file?
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Upvote 0

fabio55

Member
Licensed User
Longtime User
For who could be interested, after hours of struggling, here is a working example of downloading a file with FTP.
The tricks are:

  1. access as anonymous
  2. FTP.PassiveMode=True
B4X:
Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Dim FTP As FTP
    Private Button2 As Button
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("pluto") 'Load the layout file.
    MainForm.Show
End Sub

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
    Log(s)
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    Log(ServerPath & ", Success=" & Success)
    If Success = False Then
        Log(LastException.Message)

    End If
End Sub


Sub Button2_MouseClicked (EventData As MouseEvent)
FTP.Initialize("FTP", "ftp.cpc.ncep.noaa.gov", 21, "anonymous", "")
Log("Results:" & FTP.IsInitialized)
FTP.PassiveMode=True
If FTP.IsInitialized Then
    FTP.DownloadFile("wd52dg/data/indices/tele_index_nh", True, File.DirApp, "esempio.txt")
End If
End Sub
 
Last edited:
Upvote 0
Top