Skip to content
Andrew Lambert edited this page Jan 8, 2023 · 6 revisions

Upload a single file

This example performs a synchronous FTP upload request on the calling thread.

Dim curl As New cURLClient
Dim file As FolderItem = GetOpenFolderItem("")
Dim upload As BinaryStream = BinaryStream.Open(file)
curl.Username = "username"
curl.Password = "seekrit"
If curl.Put("ftp://ftp.example.com/bin/" + file.Name, upload) Then
  MsgBox("Upload complete.")
Else
  MsgBox(libcURL.FormatError(curl.LastError))
End If
upload.Close

Upload a directory tree

This recursive function synchronously uploads a directory using FTP. Remote sub-directories will be created automatically thanks to the libcURL.Opts.FTP_CREATE_MISSING_DIRS option.

Function FTPUpload(cURL As cURLClient, Directory As FolderItem, FTPURL As String) As Integer
  If Right(FTPURL, 1) <> "/" Then FTPURL = FTPURL + "/"
  Dim c As Integer = Directory.Count
  Dim err As Integer
  Call cURL.SetOption(libcURL.Opts.FTP_CREATE_MISSING_DIRS, True)
  
  For i As Integer = 1 To c
    Dim item As FolderItem = Directory.Item(i)
    If Not item.Directory Then
      Dim bs As BinaryStream = BinaryStream.Open(item)
      ' upload the file
      If Not cURL.Put(FTPURL + item.Name, bs) Then err = cURL.LastError
      bs.Close
      
    Else
      If cURL.Head(FTPURL) Then ' create the directory
        err = FTPUpload(cURL, item, FTPURL + item.Name + "/") ' Recurse into it
      Else
        err = cURL.LastError
      End If
    End If
    If err <> 0 Then Exit For
  Next
  
  Return err
End Function

Usage:

Dim c As New cURLClient
c.Username = "username"
c.Password = "seekrit"
Dim root As FolderItem ' the directory to upload
Dim err As Integer = FTPUpload(c, root, "ftp://ftp.example.com/public_html/")
Clone this wiki locally