Android Question Show Message after FTP download

arnold steger

Member
Licensed User
Longtime User
I want show a message when all files of my list is success downloaded.
This code not work. Erel, how can implement a counter? Writen at...
https://www.b4x.com/android/forum/threads/android-ftp-tutorial.10407/page-13#post-369277
#252
B4X:
Sub ButtonDownloadList_Click
    Dim FolderName As String    :FolderName="Namen/"

    FTP.List(FolderName)

End Sub

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    Dim FolderName As String    :FolderName="Namen/"

    Log(ServerPath)
    Log(LastException)
    ToastMessageShow("Verbindungsfehler: "&LastException, True)
    FTP.CloseNow
    Else
    ServerGeladen=ServerGeladen+1
    TimerDownload.Initialize("TimerDownload",3000)
    TimerDownload.Enabled=True

        For i = 0 To Folders.Length - 1
            Log(Folders(i).Name)
        Next    
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
            FTP.DownloadFile(FolderName&Files(i).Name,True, PfadDownload,Files(i).Name)
            ToastMessageShow(Files(i).Name&" wird geladen...",False)
        Next
        FTP.Close
    End If
    If Success AND ServerGeladen>=Files.Length Then
    ToastMessageShow(Files.Length&" Daten wurden erfolgreich geladen",False)
    TimerDownload.Enabled=False
    listViewDownloadOpen=False
    ServerGeladen=-2
    End If
End Sub

Sub TimerDownload_tick
    ToastMessageShow("wird geladen..." ,False)
  
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
You should follow another solution.
I´ll try to explain but i don´t hav an example code for you.

1. Dim a new LIST to hold all files you want to download (Globals)
B4X:
    Private lstDownload As List
2. Initialize the list in Activity Create
3. call your
B4X:
Dim FolderName As String    :FolderName="Namen/"
FTP.List(FolderName)

Inside your FTP_ListCompleted you add all files you want to download into the global list.

maybe
B4X:
    If Files.Length > 0 Then
        For i = 0 To Files.Length -1
            Dim f As FTPEntry = Files(i)
            lstDownload.Add((ServerPath&fld.Name))
        Next
    End If

After that you can start downloading the first file from the list.

B4X:
    If lstDownload.Size > 0 Then
        Dim downloadfile As String = lstDownload.Get(0)
        FTP.DownloadFile(downloadfile,False,File.DirInternal,GetFilename(downloadfile))
    End If
Just start ONE download and wait for the download complete event

Inside your downloadcomplete event you can remove the first entry from the list when the download is successfully finished.
B4X:
    If lstDownload.Size > 0 Then
        lstDownload.RemoveAt(0)
    End If

continue using downloading the next file at the end of the download completed event

B4X:
Sub ftp_DownloadCompleted (ServerPath As String, Success As Boolean)
    If Success Then
        If lstDownload.Size > 0 Then
            lstDownload.RemoveAt(0)
        End If
    End If
    If lstDownload.Size > 0 Then
        Dim downloadfile As String = lstDownload.Get(0)
        Log("Download "&downloadfile)
        LabelFile.Text = "Download("&downloadfile&")"
        FTP.DownloadFile(downloadfile,False,File.DirInternal,GetFilename(downloadfile))
    Else
        Log("TADAAA... FERTIG")
    End If
End Sub

Helper-SUB used
B4X:
Sub GetFilename(fullpath As String) As String
   Return fullpath.SubString(fullpath.LastIndexOf("/") + 1)
End Sub
 
Last edited:
Upvote 0
Top