Sample code for 30+ languages & platforms
DataFlex

Upload Text to an SFTP Server (StringBuilder)

See more SFTP Examples

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

Note: This example uses relative local paths, which are resolved against the application's current working directory. Absolute local paths may also be used. Supply the paths appropriate to your own environment.

Background: This is the text-oriented upload: you assemble a document — a CSV, a config file, JSON — in a StringBuilder and the method handles character encoding as it writes. The charset controls the bytes actually stored (for example utf-8 or iso-8859-1), and the byte-order-mark flag is useful when a consuming Windows application expects a BOM. An existing destination is truncated first.

Chilkat DataFlex Downloads

DataFlex
Use ChilkatAx-win32.pkg

Procedure Test
    Boolean iSuccess
    Handle hoSftp
    Integer iPort
    String sPassword
    Variant vSb
    Handle hoSb
    Boolean iIncludeBom
    String sTemp1

    Move False To iSuccess

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

    Get Create (RefClass(cComChilkatSFtp)) To hoSftp
    If (Not(IsComObjectCreated(hoSftp))) Begin
        Send CreateComObject of hoSftp
    End

    //  Connect, authenticate, and initialize the SFTP subsystem.
    Move 22 To iPort
    Get ComConnect Of hoSftp "sftp.example.com" iPort To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  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.
    Move "mySshPassword" To sPassword

    Get ComAuthenticatePw Of hoSftp "mySshLogin" sPassword To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Get ComInitializeSftp Of hoSftp To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    //  Build the text to upload.
    Get Create (RefClass(cComChilkatStringBuilder)) To hoSb
    If (Not(IsComObjectCreated(hoSb))) Begin
        Send CreateComObject of hoSb
    End
    Get ComAppend Of hoSb "name,quantity" + (character(10)) To iSuccess
    Get ComAppend Of hoSb "widgets,42" + (character(10)) To iSuccess
    Get ComAppend Of hoSb "gadgets,17" + (character(10)) To iSuccess

    //  Upload as UTF-8 without a byte-order mark.  If the remote file exists, it is truncated first.
    Move False To iIncludeBom
    Get pvComObject of hoSb to vSb
    Get ComUploadSb Of hoSftp vSb "subdir/inventory.csv" "utf-8" iIncludeBom To iSuccess
    If (iSuccess = False) Begin
        Get ComLastErrorText Of hoSftp To sTemp1
        Showln sTemp1
        Procedure_Return
    End

    Showln "Uploaded text file."

    Send ComDisconnect To hoSftp


End_Procedure