### Connect and Transfer

Example of connecting and transfer a file.
Libraries required: jNet
Reference: http://www.b4x.com/b4j/help/jnet.html#ftp
In simple steps for download and upload.
Download a file initiated by a Button using TextFields (txtName) and a ProgressIndicator (piMain)
`Sub btnDownload_Action
If (txtFTPRemoteFile.Text.Length = 0) Or (txtFTPLocalFile.Text.Length = 0) Then
Log("ERROR: Missing filename!")
Return
End If
Try
'Init the connection - the connection is not established. That is being done when down or upload.
Log("Initialize connection...", True)
If gFTP.IsInitialized = False Then
gFTP.Initialize("FTP", txtFTPServer.Text , 21, txtFTPUsername.Text , txtFTPPassword.Text )
End If
Log("Get filesize...")
' Get the filesize of the file to download
gFTP.List(txtFTPRemoteFile.Text)
gFTP.PassiveMode = True
piMain.Progress = 0
Log("Downloading...")
gFTP.DownloadFile(txtFTPRemoteFile.Text, False, File.DirApp, txtFTPLocalFile.Text)
Log("Close download...")
gFTP.Close
Log("Waiting for completion...")
Catch
Log(LastException.Message)
End Try
End Sub`
`Sub FTP_DownloadProgress (ServerPath As String, TotalDownloaded As Long, Total As Long)
If Total = -1 Then Total = txtFTPRemoteFileSize.Text
piMain.Progress = (TotalDownloaded / Total)
End Sub`
`Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
If Success = True Then
Log("Download completed sucessfully." & CRLF & "Status: " & Success & ", Serverpath:" & ServerPath)
Else
Log("Download not completed." & CRLF & "Error: " & LastException.Message)
End If
End Sub`
Upload a file from the server, again triggered by a Button and using TextFields and a ProgressIndicator
`Sub btnUpload_Action
Log("Checking file names...")
If (txtFTPRemoteFile.Text.Length = 0) Or (txtFTPLocalFile.Text.Length = 0) Then
Log("ERROR: Missing filename!")
Return
End If
Try
' Init the connection - the connection is not established. That is being done when down or upload.
LogMsg("Initialize connection...", True)
If gFTP.IsInitialized = False Then
gFTP.Initialize("FTP", txtFTPServer.Text , 21, txtFTPUsername.Text , txtFTPPassword.Text )
End If
' Get the filesize of the filename to upload
Log("Get filesize...")
txtFTPRemoteFileSize.Text = File.Size(File.DirApp, txtFTPLocalFile.Text)
gFTP.List(txtFTPRemoteFile.Text)
gFTP.PassiveMode = True
piMain.Progress = 0
Log("Uploading...")
gFTP.UploadFile(File.DirApp, txtFTPLocalFile.Text, False, txtFTPRemoteFile.Text)
Log("Close upload...")
gFTP.Close
Log("Waiting for completion...")
Catch
LogMsg(LastException.Message)
End Try
End Sub`
`Sub FTP_UploadProgress (ServerPath As String, TotalUploaded As Long, Total As Long)
If Total = -1 Then Total = txtFTPRemoteFileSize.Text
piMain.Progress = (TotalUploaded / Total)
End Sub`
`Sub FTP_UploadCompleted (ServerPath As String, Success As Boolean)
If Success = True Then
Log("Upload completed sucessfully. Status: " & Success & ", Serverpath:" & ServerPath)
Else
Log("Upload not completed. Error: " & LastException.Message)
End If
End Sub`

Changed: 2018-06-07 09:13, Category: FTP, File: T6C196M1439.htm, ID: 1439