User Tools

Site Tools


Sidebar

Network and communication

k24:k24.2:k24.2.3.2:start

24.2.3.2 Project 2 - FTP Client

The second project, which demonstrates the use of the FTPClient class, is specifically designed for the following use: The existing chapters of the Gambas online book are first translated into English by Emily Görnandt, then edited and finally inserted into the English version of our book at https://gambas-buch.de/dwen. She uses project 2 'DW_UPLOAD' to upload the individual translated pages in DokuWiki format. The basic structure of project 2 is taken over from project 1 and adapted especially for the file upload.

The dialogue for specifying the FTP connection data only records the FTP user name and its FTP password:

BILD 1
Figure 24.2.3.2.1: Dialogue 1 - FTP Connection Data

Note: The English text for a chapter always has the suffix '_en' in the file name.

BILD 2
Figure 24.2.3.2.2: Dialogue 2 - Selecting a translated chapter in DokuWiki format

While the local path to the upload file is determined by the file selection dialogue 2, the server path - together with the URL of the FTP server stored in the programme - is generated from the local path to the upload file. A special routine converts the file name of the upload file into 'start.txt', because all pages in DokuWiki have this file name! The assignment to the chapter is realised by the so-called 'namespace' - here k24.2.3:

BILD 3
Figure 24.2.3.2.3: The upload of the file can be started …

24.2.3.2.1 Source code dialogue 1 - FTP connection data

The dialogue for reading the FTP connection data is pleasantly short:

' Gambas class file
 
Private cFTPAccountData As Collection
Private hFTPClient As New FtpClient
 
Public Sub _call(sTitel As String) As Collection
 
    Me.Text = sTitel
 
'-- Returns if one of the two buttons was clicked! The return value is set/specified in the
'-- Me.Close() call and indicates whether the dialog was aborted or not.
 
    If Me.ShowModal() = 0 Then
       Return Null
    Else
       If txbFTPUserName.Text And If txbFTPUserPassword.Text Then
          cFTPAccountData = New Collection
          cFTPAccountData["FTPUsername"] = txbFTPUserName.Text
          cFTPAccountData["FTPPassword"] = txbFTPUserPassword.Text
          Return cFTPAccountData
       Else
          Message.Error(("The FTP account data are not complete!"))
          txbFTPUserName.SetFocus()
          Return
       Endif
    Endif
 
End
 
Public Sub Form_Open()
 
'-- Is an active network accessible?
    MAdditional.CheckNetwork()
 
    txbFTPUserName.Text = MSettings.AppSettings["FTP/Username"]
    txbFTPUserPassword.Text = MSettings.AppSettings["FTP/Password"]
 
End
 
Public Sub bConnect_Click()
    If txbFTPUserName.Text And If txbFTPUserPassword.Text Then
       Me.Close(1)
    Endif
End
 
Public Sub bCancel_Click()
    Me.Close(0)
End
 
Public Sub Form_Close()
 
    hFTPClient.Close()
 
    If rbtnRemember.Value = True Then
       MSettings.AppSettings["FTP/Username"] = txbFTPUserName.Text
       MSettings.AppSettings["FTP/Password"] = txbFTPUserPassword.Text
    Else
       MSettings.AppSettings["FTP/Username"] = ""
       MSettings.AppSettings["FTP/Password"] = ""
    Endif
 
    MSettings.AppSettings.Save()
 
End

24.2.3.2.2 Source code programme - DW_UPLOAD

The programme source code is given in full and is sufficiently commented. In contrast to project 1, all relevant events of the class FTPClient are used. The complete project archive can be found in the download area.

' Gambas class file
 
' ● Unverschlüsseltes FTP ist unsicher. Bitte wechseln Sie zu FTP über TLS oder zu SFTP.
' ● Unencrypted FTP is insecure. Please switch to FTP over TLS or to SFTP.
 
Public bConnected As Boolean
Public hFTPClient As FtpClient
Private cFTPAccountData As Collection
 
Public Sub _new()
 
    MDir.CreateBasicDirectory()
    If MDir.BasicDir Begins "/tmp" Then
       MAdditional.SetNotification("dialog-information", ("Attention"), ("The base directory is a \
                                    temporary directory!"))
    Endif
 
'-- OPTION: Folder for configuration files .../config and configuration file *.conf
    MSettings.CreateAppSettings()
 
'-- OPTION: Folder for sound files
'-- Sound files need an absolute path!
'-- Example: MAdditional.PlaySound(MDir.SoundsDir &/ "start.ogg")
    MDir.CreateSoundsDirectory()
 
End
 
Public Sub Form_Open()
 
    Dim FDLogin As New FDLogin
 
    MAdditional.CheckNetwork()
    MSettings.RestoreFormPosition(FMain)
 
    FMain.Icon = Picture["./icons/project_icon.png"]
    FMain.Caption = Upper(Application.Name) & " - VERSION: " & MSettings.AppSettings["Version/Number"]
 
    hFTPClient = New FtpClient As "hFTPClient"
 
    cFTPAccountData = FDLogin(("FTP Access Data"))
 
    If Not cFTPAccountData Then
'-- The main program is terminated without comment!
       FMain.Close()
    Else
       MAdditional.PlaySound(MDir.SoundsDir &/ "login.wav")
 
       hFTPClient.User = cFTPAccountData["FTPUsername"]
       hFTPClient.Password = cFTPAccountData["FTPPassword"]
 
   '-- OPTION
       hFTPClient.Debug = True
 
    Endif
 
End
 
Public Sub btnSelectUploadFile_Click()
 
    Dim i As Integer
    Dim sPartPath As String
    Dim sFileName, sServerURL, sServerChapterPath, sServerFileName As String
    Dim aParts As String[]
 
    txaEnglishText.Clear()
 
    Dialog.Title = ("Select a (English) DokuWiki file ...")
    Dialog.Filter = ["*.txt", "Text Files"]
 
'-- Select exactly 1 file (False -> Multiselect switched off)
    If Dialog.OpenFile(False) Then Return
 
    txbSourcePath.Text = Dialog.Path
 
'-- The original text is inserted into the TextArea (txaOriginal)
    txaEnglishText.Text = File.Load(Dialog.Path)
    txaEnglishText.SetFocus()
    txaEnglishText.Pos = 0
 
'---------------------------------------------------------------------------------------------------------
    txbSourcePath.Text = Dialog.Path
    FMain.Caption = Subst("&1  -  &2:  &3", Upper(Application.Name), ("Name of the file to upload"),
                    File.Name(Dialog.Path))
'---------------------------------------------------------------------------------------------------------
 
    aParts = Split(File.Dir(Dialog.Path), "/")
    For i = 0 To aParts.Max
      If aParts[i] Begins "k" Then sPartPath = sPartPath & "/" & aParts[i]
    Next
 
    sFileName = File.BaseName(Dialog.Path)
 
    If sFileName Ends "_en" Then sFileName = Replace(sFileName, "_en", "")
    sServerURL = "gambas-buch.de/web/dokuwiki/dwen/data/pages"
 
    sServerChapterPath = sPartPath
    sServerFileName = sFileName & ".txt"
    txbTargetURL.Text = sServerURL &/ sServerChapterPath &/ sServerFileName
 
'----------------------------------
    Prepare(txaEnglishText.Text)
'----------------------------------
 
    btnSelectUploadFile.Picture = Picture["leds/led_green_16x16.png"]
    btnFileUpLoad.Enabled = True
 
End
 
Public Sub btnFileUpLoad_Click()
 
    Dim bFTPClientStatus As Boolean = False
 
    If txbSourcePath.Text <> "" And If txbTargetURL.Text <> "" Then
    '-----------------
       FileUpload()
    '-----------------
 
       Repeat
         Wait 0.001
         lblFTPStatus.Text = "FTP-Status = " & hFTPClient.Status
       Until bFTPClientStatus = bConnected
 
    Endif
 
End
 
Public Sub hFTPClient_Connect()
    lblFTPStatus.Text = "FTP status: Connection to FTP server established!"
    bConnected = True
End
 
Public Sub hFTPClient_Finished()
    ProgressBar1.Value = 1
    btnFileUpLoad.Picture = Picture["leds/led_green_16x16.png"]
    lblFTPStatus.Text = "FTP status: Transfer complete"
    If bConnected = True Then hFTPClient.Close()
End
 
Public Sub hFTPClient_Progress()
'-- (Relative) Display of the uploaded bytes in relation to the size of the transferred file.
    If hFTPClient.TotalUploaded > 0 Then
       ProgressBar1.Value = hFTPClient.Uploaded / hFTPclient.TotalUploaded
    Endif
End
 
Public Sub hFTPClient_Error()
  Message.Error("<b><font color='DarkRed'>FTP: ERROR</b></font><hr>" & hFTPClient.ErrorText)
End
 
Public Sub hFTPClient_Read()
  Print "READ = "; hFTPClient.ReadLine
End
 
Public Sub btnClose_Click()
    FMain.Close()
End
 
Private Sub Prepare(sEnglishText As String)
 
    Dim iLineCounter, i As Integer
    Dim aLines As String[]
 
'-- Split DokuWiki source code passed as a parameter into single lines that are stored in a string array
    aLines = Split(sEnglishText, gb.NewLine)
 
'-- Remove multiple blank lines in the original source text
    For iLineCounter = aLines.Max To 1 Step -1
      If aLines[iLineCounter] = aLines[iLineCounter - 1] And IsNull(aLines[iLineCounter]) Then
         aLines.Remove(iLineCounter)
      Endif
    Next
    txaEnglishText.Clear()
 
'-- Display the English DokuWiki text in the TextArea 'txaEnglishText'
    For i = 0 To aLines.Max
      txaEnglishText.Insert(aLines[i] & gb.NewLine)
    Next
 
End
 
Private Sub FileUpload()
    hFTPClient.URL = txbTargetURL.Text
'-- The Argument in the Put(sArg) method is the path of the file to be uploaded *on the local PC*
'-- Upload the selected DokuWiki source text file:
    hFTPClient.Put(txbSourcePath.Text)
End
 
Public Sub Form_Close()
 
    MAdditional.PlaySound(MDir.SoundsDir &/ "logout.wav")
 
    MSettings.StoreFormPosition(FMain)
    If MSettings.AppSettings["Version/Number"] <> Application.Version Then
       MSettings.AppSettings["Version/Number"] = Application.Version
       MSettings.AppSettings.Save()
    Endif
 
End

Download

project

Download

The website uses a temporary session cookie. This technically necessary cookie is deleted when the browser is closed. You can find information on cookies in our privacy policy.
k24/k24.2/k24.2.3.2/start.txt · Last modified: 16.08.2022 (external edit)

Page Tools