Sample code for 30+ languages & platforms
PureBasic

Upload Text to an FTP Server (StringBuilder)

See more FTP Examples

Demonstrates the Chilkat Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads it to a remote path. The arguments are the StringBuilder, the charset, whether to include a byte-order mark, and the remote file path.

Background: The text-oriented upload: assemble a document — CSV, config, JSON — in a StringBuilder and the method handles encoding as it writes. The charset governs the bytes actually stored, and the byte-order-mark flag is useful when a consuming Windows application expects a BOM.

Chilkat PureBasic Downloads

PureBasic
IncludeFile "CkFtp2.pb"
IncludeFile "CkStringBuilder.pb"

Procedure ChilkatExample()

    success.i = 0

    ;  Demonstrates the Ftp2.PutFileSb method, which encodes the text in a StringBuilder and uploads
    ;  it to a remote path.  The 1st argument is the StringBuilder, the 2nd is the charset, the 3rd
    ;  selects whether a byte-order mark is included, and the 4th is the remote file path.

    ftp.i = CkFtp2::ckCreate()
    If ftp.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkFtp2::setCkHostname(ftp, "ftp.example.com")
    CkFtp2::setCkUsername(ftp, "myFtpLogin")

    ;  Normally you would not hard-code the password in source.  You should instead obtain it
    ;  from an interactive prompt, environment variable, or a secrets vault.
    CkFtp2::setCkPassword(ftp, "myPassword")

    success = CkFtp2::ckConnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        ProcedureReturn
    EndIf

    ;  Build the text to upload.
    sb.i = CkStringBuilder::ckCreate()
    If sb.i = 0
        Debug "Failed to create object."
        ProcedureReturn
    EndIf

    CkStringBuilder::ckAppend(sb,"name,quantity" + Chr(10))
    CkStringBuilder::ckAppend(sb,"widgets,42" + Chr(10))

    ;  Upload as UTF-8 without a byte-order mark.
    includeBom.i = 0
    success = CkFtp2::ckPutFileSb(ftp,sb,"utf-8",includeBom,"public_html/inventory.csv")
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf

    Debug "Uploaded text file."

    success = CkFtp2::ckDisconnect(ftp)
    If success = 0
        Debug CkFtp2::ckLastErrorText(ftp)
        CkFtp2::ckDispose(ftp)
        CkStringBuilder::ckDispose(sb)
        ProcedureReturn
    EndIf



    CkFtp2::ckDispose(ftp)
    CkStringBuilder::ckDispose(sb)


    ProcedureReturn
EndProcedure