B4A Library Automated FTP Library

margret

Well-Known Member
Licensed User
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.




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

Last edited:

canalrun

Well-Known Member
Licensed User
New Automated FTP Library; Currently still in Beta with version .98. Please post/report back any issues you find, etc.

This library has Total progress and Each File progress bars, handles all the muliple file and multi file types with file Masks and wildcard uploads and downloads. Issue one command and transfer multiple files and multiple types!
Hello,
Nice.

I haven't looked at your library extensively, but from what I see this looks to implement an FTP client for the Android device.

I have several apps that work with text files of words or CSV files of database records that the user can transfer to my app's directory. My app will recognize these files and import the data to the app.

Currently I tell people to install a third-party FTP server, connect from a PC, navigate to my apps directory, and transfer the file. This is cumbersome for most people.

It sure would be nice to have a tiny FTP server that I could run from my app that would allow a user to connect via a PC client and transfer files directly to my app.

Another option would be to have a tiny Web server with just enough capability to transfer a file or two if the user connects from a PC web browser.

Also, an advantage of having an embedded server over a third-party solution is that I can turn it off when done – people won't walk around with an active server listening for connections on their device.

Is something like this possible as part of your library? Does this already exist?

Thanks,
Barry.
 

margret

Well-Known Member
Licensed User

cjolly

Member
Licensed User
Hi Margret,

I Try using your library (beta 0.98) for uploading multiple files (UploadFileSet) and throws me "No Files Found. Be sure to call FTP_ListLocalFiles() first.":confused:

Anyway, congratulations on the development, will be extremely agile and useful

Tks
Charles
 

cjolly

Member
Licensed User
Sure,

'Setup to use FTP
Dim FTP As FTPClass
FTP.Initialize(Activity, "www.xxxxxxxxx.com.py", "xxx", "xxx", 21, True,True)
FTP.UploadFileSet("/var/conf/gps/files/",Array As String("/mnt/sdcard/*.log"))

that's all
 

cjolly

Member
Licensed User
Margret,

Sorry, sorry, sorry

I found the error (improperly formatted name)

Sorry, sorry, sorry

Your lib is amazing.
 

Mahares

Well Known Member
Licensed User
@Margret: Very nice piece of work:
1. Before you upload, there is stationary progress bar. After you upload, it disappears from the screen. Is there a way to keep it on the screen until the user clicks, say ok.
2. Three of the upload and download methods start with the local file on he device except the UploaFileSet. It starts with the server file. is that by design?
3. I can upload several filtered files using the UploadFileSet or using UploadFile iterating through the list, filtering and sending oe at a time. Is there a preference or does it depend on the size of the files. Hope things are well in Ga.
Thank you
 

Dataverde

Member
Licensed User
Very nice work!

I tested it with my Nexus7 as Client and a HTC Desire as Server using FPT Server App(https://play.google.com/store/apps/details?id=lutey.FTPServer).

B4X:
Sub ftpExport_click
   FTP.Initialize(Activity,all.ftpServer,all.ftpUsername,all.ftpPassword,all.ftpPort,True,False)
   FTP.UploadFileSet(all.ftpTargetDir,Array As String(all.exportPath&"*.txt"))
   FTP.Close

End Sub
There are 5 .txt files, named 1-5.txt in the export dir.
After sending the first one over (5.txt) the client stops without an error. The Blue View showing the progressbars is still displayed with "Total Progress: 1".

See the servers log:
B4X:
13-03-07T11:45:37 Starting in TETHERING mode
13-03-07T11:45:46 ==> 220 FTPServer ready (cwd is /mnt/sdcard/dataverdeUpdates/)
13-03-07T11:45:46 Acquired wakeLock 5643492581844658053
13-03-07T11:45:46 <== USER testuser
13-03-07T11:45:46 ==> 331 - Login as testuser OK. Send password
13-03-07T11:45:46 <== PASS ********
13-03-07T11:45:46 ==> 230 - Password accepted
13-03-07T11:45:46 <== TYPE I
13-03-07T11:45:46 ==> 200 Switching to mode TYPE I
13-03-07T11:45:46 <== PORT 192,168,43,25,165,197
13-03-07T11:45:46 ==> 200 PORT command successful
13-03-07T11:45:46 <== STOR /mnt/sdcard/dataverdeUpdates/5.txt
13-03-07T11:45:46 ==> 150 Binary data connection
13-03-07T11:45:46 ==> 226 transfer complete
13-03-07T11:45:46 Refreshing the MediaStore
13-03-07T11:45:46 Released wakeLock 5643492581844658053
13-03-07T11:45:46 Closing socket...
13-03-07T11:45:46 ...done
Any ideas?
 

margret

Well-Known Member
Licensed User
1. I will look into adding this option.

2. This is by design.

3. It sends the files in the order of the array holding the files.

@Margret: Very nice piece of work:
1. Before you upload, there is stationary progress bar. After you upload, it disappears from the screen. Is there a way to keep it on the screen until the user clicks, say ok.
2. Three of the upload and download methods start with the local file on he device except the UploaFileSet. It starts with the server file. is that by design?
3. I can upload several filtered files using the UploadFileSet or using UploadFile iterating through the list, filtering and sending oe at a time. Is there a preference or does it depend on the size of the files. Hope things are well in Ga.
Thank you
 

cjolly

Member
Licensed User
event problem

Hi,

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

*TIA*
 

cjolly

Member
Licensed User
Hi,

Maybe I'm doing something wrong, but I tried by all means to detect when the upload finishes and I have no results for this reason I request a complete example.:sign0085:
 

margret

Well-Known Member
Licensed User
Hi,

Maybe I'm doing something wrong, but I tried by all means to detect when the upload finishes and I have no results for this reason I request a complete example.:sign0085:
I think I see what you are trying to do. I will look into adding this feature to the next Beta of .99. In current use you know it is completed by the progress dialog. I will see if there is a clean way to do this in code.
 
Top