B4J Question FTP - name of the downloaded file

Informatix

Expert
Licensed User
Longtime User
Hello,

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.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
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
 
Last edited:
Upvote 0

Knoppi

Active Member
Licensed User
Longtime User
i dont know what
B4X:
File.DirInternal
is?

b4j-5.90 => Unknown member: dirinternal
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I guess the code is copied from a B4A project
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is simpler now with jNet v1.70: [B4X] Net library (FTP, SMTP, POP) with Wait For

B4X:
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
 
Upvote 0
Top