Android Question Downloading multiple files

JamesGreaves

Active Member
Licensed User
I have created a list of files on the server.
I them iterate through the list to download them.
The files are created, but they are empty.
Not sure what I am doing wrong, but have a feeling it has something to do with having to wait for each file to download, before downloading the next.

B4X:
Sub DownloadListOfBuildings
    ProgressDialogShow2("Downloading Building Names" ,False)
    
    FTP.Initialize("FTP", FtpServer, 21, FtpUsername, FtpPassword)
    
    Dim ServerPath As String:     ServerPath ="/"
    FTP.List(ServerPath)
    
End Sub

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    
    Dim FileName As String
    
    For i = 0 To Files.Length -1
        FileName = Files(i).Name
        
        Select Case FileName
            Case ".","..",".ftpquota"
            Case Else:             
            FTP.DownloadFile("/",True, File.DirInternal, FileName)
        End Select
        
    Next
    
    ProgressDialogHide
End Sub
 

DonManfred

Expert
Licensed User
Longtime User
but have a feeling it has something to do with having to wait for each file to download, before downloading the next
not really. but you need to keep in mind that you are starting a downloadtask for each file you have. If there are 1000 files you are starting 1000 tasks.
Note that the download starts after the sub
FTP_ListCompleted
finishes.

You can use wait for to wait for the download to finish.

A similar task is done using okhttputils here:
https://www.b4x.com/android/forum/threads/b4x-okhttputils2-with-wait-for.79345/#content
check the "downloadmany"

It should be easy to rewrite it to use FTP instead.
 
Upvote 0

JamesGreaves

Active Member
Licensed User
I am struggling to understand how to trigger the end of the Wait for, DownloadComplete.

B4X:
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    
    Dim FileName As String
    
    For i = 0 To Files.Length -1
        FileName = Files(i).Name
        FTP.DownloadFile("/",True, File.DirInternal, FileName)
        
        Wait for (FTP) DownloadCompleted (ServerPath As String, Success As Boolean)
        
    Next
    
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
    
End Sub
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub DownloadFolder (ServerFolder As String)
   FTP.List(ServerFolder)
   Wait For FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry) '<----
   If Success Then
     For Each f As FTPEntry In Files
         FTP.DownloadFile(ServerPath & f.Name, False, File.DirApp, f.Name)
         Wait For FTP_DownloadCompleted (ServerPath2 As String, Success As Boolean) '<-----
         Log($"File ${ServerPath2} downloaded. Success = ${Success}"$)
     Next
   End If
   Log("Finish")
End Sub
 
Upvote 0
Top