I'm trying the FTP library and I don't know how to get the path of the downloaded file in the DownloadCompleted event. The event returns the path on the server, but not the path of the local file. I could maintain a map (this server path = this local path) but this solution has a serious limitation: if I want to download a file in two different locations (it's a required feature of the created app), how do I distinguish between them? Since this FTP lib is able to download more than one file at once, it should exist a way to identify and locate the downloaded file, but I don't find it.
FTP is a synchronous protocol. There could be only a single file transfer at a time. The library makes sure that only a single task runs at any given time (the other tasks will wait).
If you are sending many requests at the same time then theoretically the order of responses might not match the order of requests. It is not very likely to happen but it can.
You can use code such as this one to manage all downloads:
B4X:
Sub DownloadFile (ServerPath As String, LocalDir As String, LocalName As String) As ResumableSub
Do While FTPIsBusy
Sleep(500)
Loop
FTPIsBusy = True
FTP.DownloadFile(ServerPath, False, LocalDir, LocalName)
Wait For FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
FTPIsBusy = False
Return Success
End Sub
FTPIsBusy is a global variable.
You can then call it anywhere you like with:
B4X:
Wait For (DownloadFile("/1.txt", File.DirInternal, "1.txt")) Complete (Success As Boolean)
If Success Then
Log("/1.txt was downloaded successfully")
End If
That's true. I wrote it with B4A in mind. The DownloadFile sub will also work with B4A and B4i. Note that in B4A the best place for such code is in a class that is initialized in Service_Create of the Starter service.
Sub DownloadFile (ServerPath As String, LocalDir As String, LocalName As String) As ResumableSub
Dim sf As Object = FTP.DownloadFile(ServerPath, False, LocalDir, LocalName)
Wait For (sf) FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
Return Success
End Sub