Example of a simple working FTP.List code

Asmoro

Active Member
Licensed User
Longtime User
Hi All,

I searched and tried several FTP.List examples and how to's
from everyone here on this forum.
It didn't work for me though, unfortunately.

The only thing that worked a bit is an empty imagefile??
If someone knows the issue, I will gladly here from you.

Thanks for now.
 

Asmoro

Active Member
Licensed User
Longtime User
Hi Erel,

As a matter of fact, none of these I'd got.

Yes, most of the time I use a real device for testing.

No, my Filezilla client is in automode for active or passive
and in Binary, so no issue with these.
The proof was that I succeeded to download a specific
imagefile from my server to my phone.
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi vb1992,

I tried that already, didn't work.

I also tried with FTP.List("public_html/Test2/") as a serverpath and worked, but didn't go the a specific folder on the sdcard. It went on the sdcard instead.

This is what I have so far:

B4X:
Sub btTest_Click    'ListFiles (Dir As String) As List
   ImageFolder2 = File.DirRootExternal & "/Test1/"
   Files.Initialize
   Files = File.ListFiles(ImageFolder2)
   Files.SortCaseInsensitive(True)
   
   FTP.List("public_html/Test2/")   
   FTP.PassiveMode = True
End Sub
Sub   FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
   Dim s As String
   s = "public_html/Test2/" & Round(TotalDownloaded / 1000) & "KB"
   If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
   Log(s)
End Sub

Sub   FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
   Log(ServerPath & ", Success=" & Success)
   If Success = False Then Log(LastException.Message)
   
End Sub
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
       Log(ServerPath)
      If Success = False Then
           Log(LastException)
      Else
          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))
           Next
       End If
End Sub

How did you manage with the "empty" variable on the: "
B4X:
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
"

After some changes in the code, nothing worked anymore.
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
Tips:

1) Add latest NET Library

2) Make Sure you don't have other FTP test apps running (Reboot phone if needed)

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   
   
   Dim FTP As FTP
   
   
   
End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

End Sub

Sub Activity_Create(FirstTime As Boolean)
 
 Activity.Title= "Use the Menu to List the Files"
 Activity.AddMenuItem("LIST FILES","menuLIST")
 
 
End Sub

Sub Activity_Resume

If FTP.IsInitialized Then FTP.closenow


  FTP.Initialize("FTP", "FTP.secureftp-test.com", 21, "test", "test")
End Sub



Sub Activity_Pause (UserClosed As Boolean)
FTP.Close
End Sub



Sub menuLIST_Click
ToastMessageShow("Checking for FTP List now",False)
FTP.PassiveMode = True

   FTP.List("./")
End Sub
 
   Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
       Log(ServerPath)
       If Success = False Then
           Log(LastException)
       Else
           For i = 0 To Folders.Length - 1
         ToastMessageShow(Folders(i).Name,False)
            Log(Folders(i).Name)
           Next
           For i = 0 To Files.Length - 1
               Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
               ToastMessageShow(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp),False)
            
        Next
       End If
   End Sub
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi vb1992,

Thanks for your input.

I did all your tips but I still get some error that I'd mentioned it befor:


Any idea's??
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Well, I made a test project regarding ftp.list downloading imagefiles as been asked.

It works (not perfect though) according to the logs confirmation, but no imagefiles
on my sdcard folder "File.DirRootExternal & "/Test/" yet.

So, I hope someone knows the answer, cause I'm already fed up with this. :sign0148:


note: to get this project working (attached), use your own ftp location etc
and create some folder in your sdcard. That's it.
 

Attachments

  • FTP.List download test.zip
    5.7 KB · Views: 436
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
This will download the file correctly
I named the file: 999999999999.zip
because it's a lot easier to find when your searching around
your android device with a file manger

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.
   
   Dim FTP As FTP

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   
   Dim btn_download As Button
   Dim btn_list As Button
   Dim btn_Upload As Button
   Dim Button1 As Button
   Dim Button2 As Button
End Sub

Sub Activity_Create(FirstTime As Boolean)

   Activity.LoadLayout("Main")
   
   
End Sub

Sub Activity_Resume
 FTP.Initialize("FTP", "FTP.secureftp-test.com", 21, "test", "test")
End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

   

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
Dim s As String
s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
Log(s)
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
Log(ServerPath & ", Success=" & Success)
If Success = False Then Log(LastException.Message)
End Sub



Sub btn_download_Click
   FTP.PassiveMode = True
    msgbox("File will be stored in: " &  File.DirRootExternal,"")
   FTP.DownloadFile("hamlet.zip", False, File.DirRootExternal, "999999999999.zip")
      
End Sub

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)

   If Success == False Then
      Log(LastException.Message)
   Else
      For i = 0 To Files.Length - 1
         Log(Files(i).Name)
      Next
      For i = 0 To Folders.Length - 1
         Log(Folders(i).Name)
      Next
   End If
End Sub
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Thanks again vb,

One question though , according to your code for downloading, does it mean that I have to change this:

B4X:
FTP.DownloadFile("hamlet.zip", False, File.DirRootExternal, "999999999999.zip")

to:

B4X:
FTP.DownloadFile(".jpg", False, File.DirRootExternal, ".jpg")

getting from a specific folder all the .jpg imagefiles from the server ?

Cause that's what I'm trying to achieve here.

btw. I'm not testing your code while writing this.
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
Test with this, and it will work, then modify it
for your own server/project and change .xml to .jpg


B4X:
Sub Process_Globals
 
    Dim FTP As FTP

End Sub



Sub Globals
    
    Dim btn_download As Button
    Dim btn_list As Button
    Dim btn_Upload As Button
    Dim Button1 As Button
    Dim Button2 As Button

End Sub

Sub Activity_Create(FirstTime As Boolean)
 
 Activity.Title= "Use the Menu to Download the JPG Files"
 Activity.AddMenuItem("Download JPG Files","menuLIST")
 
 
End Sub

Sub Activity_Resume

If FTP.IsInitialized Then FTP.closenow
  FTP.Initialize("FTP", "FTP.secureftp-test.com", 21, "test", "test")
  
End Sub



Sub Activity_Pause (UserClosed As Boolean)
FTP.Close
End Sub



Sub menuLIST_Click


    Msgbox("File will be stored in: " &  File.DirRootExternal,"")
   
ToastMessageShow("Checking for FTP List now",False)
FTP.PassiveMode = True

   FTP.List("./")
   
End Sub
 
   Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
       Log(ServerPath)
       If Success = False Then
           Log(LastException)
       Else
           For i = 0 To Folders.Length - 1
         ToastMessageShow(Folders(i).Name,False)
            Log(Folders(i).Name)
           Next
           For i = 0 To Files.Length - 1
               Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
               ToastMessageShow(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp),False)
         
            If Files(i).Name.EndsWith(".xml") Then FTP.DownloadFile(Files(i).Name, False, File.DirRootExternal, Files(i).Name)
        
      Next
       End If
   End Sub
   
   
   
   
   
 
   

Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
Dim s As String
s = "Downloaded " & Round(TotalDownloaded / 1000) & "KB"
If Total > 0 Then s = s & " out of " & Round(Total / 1000) & "KB"
Log(s)
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
Log(ServerPath & ", Success=" & Success)
If Success = False Then Log(LastException.Message)
End Sub
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi vb1992,

I implemented your code for testing.
It worked well but again an empty imagefile
on my sdcard.

Underneath is the log output of the error:


After that I was searching on the internet if it has something
to do with the server config.
On the other hand it was ok with the FTP.DownloadFile code
retrieving a specific imagefile.

So I'm a little bit confused.

Could it be with the code what you had made?
I also changed the .xml to .jpg.
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
Let's start with the basics, let's get the example code to work as is, since I know that works


a) Did you test the code as is, and are you able to get and write
the xml files to your sd card?


b) could be with writing to your sd card?
see: writing-external-storage-sd-card.html#post52341
 
Last edited:
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Well,

It seems that has nothing to do with your code or mine.
Probably yours working allright, so my deep :sign0188:

It has to do with how Filezilla or any kind of ftp server works
with retrieving/sending multiple files.

I explained at the Filezilla forum what the problem was, and
this is the answer in return:

:sign0148:

Erel did post something at the forum about FTP.Send Command(pwd,...)
Maybe that's the way how has to be done.

If you know it, share it.:sign0163:
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
I believe that the code does/will work for you

You can add

B4X:
     If Files(i).Name.EndsWith(".jpg") Then 

        FTP.SendCommand("binary")

        FTP.DownloadFile(Files(i).Name, False, File.DirRootExternal, Files(i).Name)

end if

Since you said the jpg file was empty
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User

Sorry, didn't work.

I'm gonna fetch your example and will let you know a.s.a.p.
 
Upvote 0

Asmoro

Active Member
Licensed User
Longtime User
Hi vb1992,

I tried your example and get the same problem.
(empty image files and log errors)

After that I Googled a little and saw a Linux command ftp like:

ftp> mget *.jpg

With mget you can download multiple images. This command downloads all files that end with ".jpg".

And typing this in FTP.SendCommand("mget", "*.jpg") --> correct me if
this is a wrong syntax/code.
 
Upvote 0

vb1992

Well-Known Member
Licensed User
Longtime User
This seems to be something with your ftp server configuration.
I would suggest you trying some other FTP Server software.

I don't think changing a command is going to solve your problem
since you already have some valid working code

The example project works, and I tested it both downloading
xml files, and also a binary / zip file
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…