B4A Library Automated FTP Library

New Automated FTP Library


UPDATED 3-24-2014 - Version 1.06 changes the display scale and should look better on the larger display of a tablet. A sample project is now included which shows how to use this library. The sample code requires B4A 3.2+. You can open and view the code with older versions but it will not run because of the newer layout files.


UPDATED 1-24-2014 - Version 1.05 adds a new function for checking for files or folders. It also contains a few other small fixes.
The new command works for Files or Folders. If the last parameter is set to TRUE, it will look for folders, if the last parameter is set to FALSE it will look for files. Because the code runs in the background and is modeless you must have a callback sub in the calling module. Code samples below:

B4X:
'Call the command like this:
FTP.FileOrFolderExist("plane.jpg" , "test/files", False)

B4X:
'Callback sub in calling module:
Sub FTP_FileExist(Found As Boolean)
    If Found Then Log("True") Else Log("False")
End Sub

UPDATED 4-2-2013 - Version 1.02 Corrects an issue where PassiveMode was not working and also corrects an issue where UploadFileSet would not work from File.DirInternal.

Automates the process of sending and receiving files using FTP with Wildcards & File Masks. Version 1.02 is attached. Please post/report back any issues you find, etc.

This library has progress bars for Each File and overall progress. It handles wildcards and multi file types at the same time. Issue one command and transfer multiple files and multiple types! Change the colors, text size and words of all objects on the Progress Dialog to fit the look of your app. Also works in Stealth Mode!

REQUIRES: B4A 2.5 +
Copy the FTP_Auto.jar & FTP_Auto.xml files to the Extra Libraries folder.


ss.png


FTP_Auto
  • Close
    Completes the current Que with any files left to process and then
    closes the connection.
  • CloseNow
    Closes the connection and all other files in the Que are
    ignored and the process is terminated.
  • DeleteFile (DeletePathAndFile As String)
    Deletes a single file from the server.
    Example:

    DeleteFile("public/pictures/airplane1.jpg")
  • DeleteFileSet (VerifyBeforeDelete As Boolean)
    Delete the lastest FileSet in memory from the server.
    If you used a Mask to download a set of files, this function
    will delelete all files in that last set.
    NOTICE: use with care.
  • DownLoadFile (LocalPath As String, ServerPath As String, FileName As String, Delete_FromServer As Boolean)
    Downloads a selected file from the FTP server and optionally deletes the file after successful download
    Example:

    DownLoadFile("/mnt/sdcard/pictures/", "public/downloads/", "airplane.jpg", False)
  • DownloadFileSet (LocalPath As String, Files_Path_And_Mask() As String, Delete_FromServer As Boolean)
    Retrieves a list of files from the server matching the Mask(s) passed to Files_Path_And_Mask() array.
    You should include the full server path in the Mask. It will then download these files to
    the Folder passed in LocalPath. Files_Path_And_Mask() can contain one entry or however many is needed.
    Example:

    DownloadFileSet(File.DirInternal, Array As String("SM*.txt", "public/pictures/pla*.jpg"), False)
  • IsInitialized As Boolean
    Tests whether the object has been initialized.
  • Initialize (Activity As Activity, CalledFrom As Object, FTP_Site As String, UserName As String, PassWord As String, PortNumber As Int, ShowMessages As Boolean, UsePassiveMode As Boolean) As String
    Initializes the object for use. If True is passed for ShowMessages, additional
    messages will be displayed throughout the process. Use the keywords "Activity"
    and "Me" For the first two parameters.
    Example:

    Initialize(Activity, Me, "ftp.yourdomain.com", "userName", "PassWord", "Port", True, False)
  • LastDownloadStats ( As ) As String
    Shows a popup window with the detials of the latest Download(s).
  • LastProcessSuccessful As Boolean
    Returns True if the last Upload or Download process was successful, otherwise returns False.
  • LastUploadStats ( As ) As String
    Show a popup window with details of the latest Upload(s).
  • MakeDir (DirectoryName As String)
    Makes a directory on the FTP server. Does nothing if the directory already exist.
    If you want to make a directory in the root of the server and then a directory under
    it, you must issue the command twice.
    Example:

    MakeDir("test")
    MakeDir("test/datafolder")
  • RemoveDir (DirectoryName As String)
    Removes selected directory from the FTP server
  • SetColors (HeadingColor As Int, TotalProgressColor As Int, FileTextColor As Int, PanelBackgroundColor As Int, FrameColor As Int)
    Used to set colors of each Item in the status dialog.
    NOTE: Passing 0 for any argument with leave that item unchanged
  • SetCompletedDownloadDialog (ShowCompletedDialog As Boolean, DialogText As String)
    If ShowCompletedDialog=True, it will show a dialog after the download process is completed.
  • SetCompletedUploadDialog (ShowCompletedDialog As Boolean, DialogText As String)
    If ShowCompletedDialog=True, it will show a dialog after the upload process is completed.
  • SetProcessCompleteOn (ProcessCompleteOn As Boolean)
    Use this function to determine by code when the Upload or Download is finished.
    The calling Activity must contain a sub named FTP_ProcessComplete. If your calling
    Activity includes this sub and you set this function to True, then the sub will
    be called as soon as the process has completed. EXAMPLE:

    'Add this sub to calling Activity
    Sub FTP_ProcessComplete
    'Your code here...
    End Sub
  • SetStealthOn (UseStealth As Boolean)
    If set to True, no progress box will be displayed during the file
    transfer. If False the progress dialog will show during the file
    transfers. The default is False.
  • SetStickyOn (MakeSticky As Boolean)
    If set to True, the progress box will stay on the screen until
    the user taps the Done button, otherwise the progress box will close
    as soon as the transfer is complete. The default is False.
  • SetText (HeaderDownLoadText As String, HeaderUploadText As String, TotalProgressText As String, FileProgressText As String, ButtonCancelText As String, ButtonDoneText As String)
    Used to set the Text of the Item's status display.
    NOTE: May be used for different language, etc.
  • SetTextSize (HeaderTextSize As Int, TotalProgressTextSize As Int, FileProgressTextSize As Int, CancelAndDoneTextSize As Int)
    Used to set the Text size of the Item's Text in the status dialog.
    NOTE: Passing 0 for any argument with leave that item unchanged
  • UpLoadFile (LocalPath As String, ServerPath As String, FileToSend As String)
    Uploads a single file passed in FileToSend

    Example: UpLoadFile("/mnt/sdcard/pictures", "uploads/pictures", "vacation.jpg")
  • UploadFileSet (ServerPath As String, Files_Path_And_Mask() As String, DeleteFileFromLocalDevice As Boolean)
    Retrieves a list of files from the local device. The Files_Path_And_Mask() array should contain the full
    path to the local devices files. It will then upload these files to the server.
    Files_Path_And_Mask() array can contain one entry or however many is needed.
    Example:

    UploadFileSet("uploads/pictures/", Array As String("/mnt/pictures/VA*.jpg", "/mnt/sdcard/backups/contact*.dat"), False)
  • WiFi_IsConnected As Boolean
    Check if Wifi is connected
    Returns True if connected otherwise returns False

Permissions:
◦android.permission.ACCESS_WIFI_STATE
◦android.permission.INTERNET
 

Attachments

  • Automated FTP Version 1.05.zip
    14.8 KB · Views: 973
  • Automated FTP Version 1.06.zip
    14.3 KB · Views: 1,619
  • FTP_Auto_Sample_Code_1.06.zip
    322.9 KB · Views: 1,778
Last edited:

Dataverde

Member
Licensed User
Longtime User
Right now what it does is:
1. display an errormessage
2. run call the FTP_ProcessComplete sub

I would like do handle stuff differently based on the succsess of the operation.

If the upload went well:
- notifie the user that "all is fine"
- move the uploaded files to a backup folder localy
else
- notifie the user that something went wrong


therefore i need an indicator in the FTP_ProcessComplete sub that shows me if the last queue was processed successful.
or some other way to find out if there was an error during upload/download something like ftp.getLastError for example.
 

margret

Well-Known Member
Licensed User
Longtime User
OK, I see what you are looking for, I will add this function, I think it is a good Idea!



Right now what it does is:
1. display an errormessage
2. run call the FTP_ProcessComplete sub

I would like do handle stuff differently based on the succsess of the operation.

If the upload went well:
- notifie the user that "all is fine"
- move the uploaded files to a backup folder localy
else
- notifie the user that something went wrong


therefore i need an indicator in the FTP_ProcessComplete sub that shows me if the last queue was processed successful.
or some other way to find out if there was an error during upload/download something like ftp.getLastError for example.
 

cjolly

Member
Licensed User
Longtime User
:BangHead:

Thank you very much, I must be blind not to have seen.

Feature request:

In the same way that DownloadFileSet (LocalPath As String, Files_Path_And_Mask () As String, Delete_FromServer As Boolean) can delete the file on download success, you can also delete the file on success upload UploadFileSet (ServerPath As String, Files_Path_And_Mask () As String, Delete_UploadFile as Boolean)...
 

cjolly

Member
Licensed User
Longtime User
Exactly, that would be a tremendous help that functionality.
 

doncx

Active Member
Licensed User
Longtime User
Margret -

I'm very excited at the prospect of putting your library to work.

One thing is tripping me up; the second parameter to Initialize.

What is "CalledFrom As Object"?

Actually, I now see my problem is greater. I can't seem to Dim the FTP object. I've tried "Dim FTP As FTP" and also "Dim FTP As FTP_Auto". The first statement only works when I include the "Net" library. When I don't include the Net library, neither statement compiles. It seems too simple a problem. What am I doing wrong?

Many thanks for your reply.

Wait, wait! I think I'm getting somewhere. "FTP" appears to be reserved. FTP1 is working Dimmed as FTP_Auto. What, then, is "CalledFrom"?
 
Last edited:

HDSInformatics

Member
Licensed User
Longtime User
Hi,


Is presume that you have some example files? Would someone be so kind and help me out with some code? I need to use the library to Download a file from an ftp server, the progressbar that pops up. And Deletes the file from this server and same with uploading but the other way around of course.
I don't know how to start writing code for this. I have all this working but with the NET library but I want this one...

Anyone?

Michael
 

HDSInformatics

Member
Licensed User
Longtime User
Rename upload file

I finally got things to work.
For the people that want to change the name of the file they send back to their ftp server, you can simply add this code to you Upload_Click sub.

Example
B4X:
Sub Upload_Click
   
   RenameFile("/mnt/sdcard/mapftp/" & strSerialnumber, "/mnt/sdcard/mapftp/" & strSerialnumberUpload)

    FTP.UploadFile("/mnt/sdcard/mapftp", ""  , strSerialnumberUpload)

The strings I use are irrelevant for you.

You all would certainly know this, but I would just like to help as much as I can.

Michael
 

doncx

Active Member
Licensed User
Longtime User
Thanks Margret. I will use "Me" as the CalledFrom Object. My biggest problem was using a reserved word (FTP) for my instantiation. After fixing that, I realized I could send a null string for the CalledFrom parameter and all works incredibly easily.

Your library is fabulous. Thank you, Thank you.
 

HDSInformatics

Member
Licensed User
Longtime User
SetProcessCompleteOn Error

Hi,

I keep getting an error

An error has occurred in sub:
jave.lang.ClassNotFoundException: b4a.example.ftp
Continue?
Yes & No

Here is my code that is the cause of that error

B4X:
Sub Download_Click
If FTP.WiFi_IsConnected Then
      FTP.SetProcessCompleteOn(True)
      FTP.DownLoadFile("/mnt/sdcard/mapftp/", "", strSerialnumber, False)
      FTP.SetCompletedDownloadDialog(True,"Download succesvol")
         
   Else
      Msgbox("Message......","Error")
   End If
   

End Sub

B4X:
Sub FTP_ProcessComplete
   
   
   If FTP.WiFi_IsConnected Then
      FTP.SetStealthOn(True)
      FTP.upLoadFile("/mnt/sdcard/mapftp/", "Downloaded", strSerialnumber)
   End If
   
End Sub
When I put the FTP.SetProcessCompleteOn(True) in my Download Button the error keeps popping up when the download is finished. And it only downloads once instead of x times without the FTP.SetProcessCompleteOn(True) code.

Thanks

Michael
 

margret

Well-Known Member
Licensed User
Longtime User
Try the code below. Do your settings before the Upload or Download. As you see I changed the order you called the settings. In the upload I added a / to the end of the directory name. See if this code works. The reason you keep getting the error is that both upload and download call the FTP_ProcessComplete. So when there is an error in the upload, it calls the ProcessComplete which calls the upload which calls the ProcessComplete and it keeps going... You should not do uploads and downloads in ProcessComplete as this can be the result. However, I am adding a function to tell if there was an error and this way you can use it and not call the upload again.

B4X:
Sub Download_Click
If FTP.WiFi_IsConnected Then
      FTP.SetProcessCompleteOn(True)
      FTP.SetCompletedDownloadDialog(True,"Download succesvol")
      FTP.DownLoadFile("/mnt/sdcard/mapftp/", "", strSerialnumber, False)
   Else
      Msgbox("Message......","Error")
   End If
End Sub

B4X:
Sub FTP_ProcessComplete
   If FTP.WiFi_IsConnected Then
      FTP.SetStealthOn(True)
      FTP.upLoadFile("/mnt/sdcard/mapftp/", "Downloaded/", strSerialnumber)
   End If
End Sub
 
Last edited:

margret

Well-Known Member
Licensed User
Longtime User
New version 1.01 posted in post #1 of this thread. It includes two new features that were requested. The documentation in post #1 has also been updated. The two new features are listed below. In the UploadFileSet the last option is new: DeleteFileFromLocalDevice.

NOTICE: The name of the .jar and .xml file has changed from FTP.* to FTP_Auto.*. If you were using a previous version, please delete the FTP.jar and the FTP.xml files from the extra libraries folder.

  • LastProcessSuccessful As Boolean
    Returns True if the last Upload or Download process was successful, otherwise returns False.
  • UploadFileSet (ServerPath As String, Files_Path_And_Mask() As String, DeleteFileFromLocalDevice As Boolean)
    Retrieves a list of files from the local device. The Files_Path_And_Mask() array should contain the full
    path to the local devices files. It will then upload these files to the server.
    Files_Path_And_Mask() array can contain one entry or however many is needed.
    Example:

    UploadFileSet("uploads/pictures/", Array As String("/mnt/pictures/VA*.jpg", "/mnt/sdcard/backups/contact*.dat"), False)
 
Last edited:

HDSInformatics

Member
Licensed User
Longtime User
Try the code below. Do your settings before the Upload or Download. As you see I changed the order you called the settings. In the upload I added a / to the end of the directory name. See if this code works. The reason you keep getting the error is that both upload and download call the FTP_ProcessComplete. So when there is an error in the upload, it calls the ProcessComplete which calls the upload which calls the ProcessComplete and it keeps going... You should not do uploads and downloads in ProcessComplete as this can be the result. However, I am adding a function to tell if there was an error and this way you can use it and not call the upload again.

B4X:
Sub Download_Click
If FTP.WiFi_IsConnected Then
      FTP.SetProcessCompleteOn(True)
      FTP.SetCompletedDownloadDialog(True,"Download succesvol")
      FTP.DownLoadFile("/mnt/sdcard/mapftp/", "", strSerialnumber, False)
   Else
      Msgbox("Message......","Error")
   End If
End Sub

B4X:
Sub FTP_ProcessComplete
   If FTP.WiFi_IsConnected Then
      FTP.SetStealthOn(True)
      FTP.upLoadFile("/mnt/sdcard/mapftp/", "Downloaded/", strSerialnumber)
   End If
End Sub


Thank you for your help, but i'm still doing something wrong. It keeps giving me the same error.
Here is a picture of my logs.
x0y68z.jpg


And my code

B4X:
Sub Upload_Click

'Renames the file from serial number to SN_Date_Time
   RenameFile("/mnt/sdcard/mapftp/" & strSerialnumber, "/mnt/sdcard/mapftp/" & strSerialnumberUpload)
'    FTP.UploadFile("/mnt/sdcard/mapftp", ""  , strSerialnumberUpload)
   
   If FTP.WiFi_IsConnected Then
        FTP.SetCompletedUploadDialog(True, "Upload succesvol")
      FTP.LastProcessSuccessful
      FTP.UploadFile("/mnt/sdcard/mapftp", ""  , strSerialnumberUpload)
      
    Else
        Msgbox("Message....... ","Error")
      '  FTP.CloseNow
    End If
   
End Sub

B4X:
Sub Download_Click

   If FTP.WiFi_IsConnected Then
      FTP.SetProcessCompleteOn(True)
      FTP.LastProcessSuccessful
      FTP.SetCompletedDownloadDialog(True,"Download succesvol")
      FTP.DownLoadFile("/mnt/sdcard/mapftp/", "", strSerialnumber, False)      
   Else
      Msgbox("Message ........","Error")
   End If

End Sub

B4X:
Sub FTP_ProcessComplete

   If FTP.WiFi_IsConnected Then
      FTP.SetStealthOn(True)
      FTP.upLoadFile("/mnt/sdcard/mapftp/", "Downloaded/", strSerialnumber)
   End If   

End Sub

In the code above I removed all my strings to make the code shorter for you..
And I call my FTP.Initialize in my Activity.Create sub.

Does it have something to do with how I organize my strings or where I call my Initialize?

Michael
 

lorebarita

Member
Licensed User
Longtime User
use with SFTP

Hi Margret can this be working with SFTP? Or maybe you might think to add the feature, since security is always an issue with FTP.
Thanks
Lorenzo
 

cjolly

Member
Licensed User
Longtime User
Hi Margret

Great, the DeleteFileFromLocalDevice work's OK. :wav:

Amazing job you did,

Muchas, muchas gracias.

Charles
 

HDSInformatics

Member
Licensed User
Longtime User
Hi,

For some reason the event UploadCompleted does not work, you could post a complete example?

*TIA*
I'm not sure if you already received an answer. But UploadCompleted is used in the NET library not the FTP_Auto. You should use LastProcessSuccessful . Not as a sub of course.

Michael
 

cjolly

Member
Licensed User
Longtime User
Hi Michael,

With the latest lib version (thanks again Margret), everything works perfectly, the library is very simple/fast to use and helped me a lot with the simplification of sending/receiving files to/from FTP server. again :wav:
 
Top