Sample code for 30+ languages & platforms
Classic ASP

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 Classic ASP Downloads

Classic ASP
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<body>
<%
success = 0

'  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.

set sftp = Server.CreateObject("Chilkat.SFtp")

'  Connect, authenticate, and initialize the SFTP subsystem.
port = 22
success = sftp.Connect("sftp.example.com",port)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
    Response.End
End If

'  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.
password = "mySshPassword"

success = sftp.AuthenticatePw("mySshLogin",password)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
    Response.End
End If

success = sftp.InitializeSftp()
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
    Response.End
End If

'  Build the text to upload.
set sb = Server.CreateObject("Chilkat.StringBuilder")
success = sb.Append("name,quantity" & vbLf)
success = sb.Append("widgets,42" & vbLf)
success = sb.Append("gadgets,17" & vbLf)

'  Upload as UTF-8 without a byte-order mark.  If the remote file exists, it is truncated first.
includeBom = 0
success = sftp.UploadSb(sb,"subdir/inventory.csv","utf-8",includeBom)
If (success = 0) Then
    Response.Write "<pre>" & Server.HTMLEncode( sftp.LastErrorText) & "</pre>"
    Response.End
End If

Response.Write "<pre>" & Server.HTMLEncode( "Uploaded text file.") & "</pre>"

sftp.Disconnect 

%>
</body>
</html>