FTP: How to Make a ProgessBar Perform Properly When Downloading Multiple Files

Mahares

Expert
Licensed User
Longtime User
I am downloading 7 files that must begin with a given string from a server to a real device using FTP. The total size of the files combined is a little more than 1 MB. The files download properly, but the progress bar seem to hit 100% almost instantly which appears to be too fast. The toastmessage lags too far behind. Below is the code I use for the download and the progressbar calculation:
B4X:
Dim MyFile(100) As String  'names of files on server
Dim i, n as Int
Dim DBFilePath as string   'Device file path
Dim pb As ProgressBar
Dim CurrentFileSize As Int
Dim TargetedFile As String
Dim GrandTotalDownloaded, TotalDownload  As Long

'DOWNLOAD BUTTON CLICK
Sub btnDownload_Click
      For i = 0 To n - 1   'n is the total number of files in folder returned from FTP_ListCompleted
           If MyFile(i).StartsWith("Base_")  Then  
           'Below Export is the alias of the server folder containing the files
            FTP.DownloadFile("Export/" & MyFile(i) , False, DBFilePath, MyFile(i))         
            TargetedFile=MyFile(i)
            CurrentFileSize = File.Size(DBFilePath, TargetedFile)         
            GrandTotalDownloaded=GrandTotalDownloaded + CurrentFileSize
            ToastMessageShow("File downloaded: " & MyFile(i),False)            
         End If
      Next         
End Sub   

'DOWNLOAD PROGRESS
Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
    Dim s As String
    s = "Downloaded " & Round(TotalDownloaded / 1024) & " KB"
    If Total > 0 Then s = s & " out of " & Round(Total / 1024) & " KB"
    Log(s)
      TotalDownload=TotalDownload + TotalDownloaded
      pb.Progress = 100 * TotalDownload / GrandTotalDownloaded
End Sub

'DOWNLOAD COMPLETED
   Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
       Log(ServerPath & ", Success=" & Success)
       If Success = False Then 
            Log(LastException.Message)
            Msgbox("DOWNLOAD OF BASE FILES FAILED","DOWNLOAD FAILED")
            Return
      End If
      'Msgbox("TOTAL DOWNLOADED: " & Round(GrandTotalDownloaded/1024) &  " KB.","")
   End Sub
Thank you for any ambiguity you see in my code.
 

Mahares

Expert
Licensed User
Longtime User
you should look into calling the next download from the ftp download complete event
Not clear.
My thanks to Margret. I look forward to taking a crack at Margret Class when it is available. In the meantime, has anyone tried to FTP download a few files in succession, where they can shed some insight with more details? My objective is simple: Cycle through the given files in the server, download them to the device one after the other, but make sure the PROGRESSBAR is displaying the proper download progress, while the files are downloading.
Thank you
 
Upvote 0
Top