C# SFTP Upload from Byte[]

This example demonstrates how to open a remote file on an SSH/SFTP server,
write to the file, and then close it.  This is analogous to opening a local file,
writing to it, and closing it.  The SFTP protocol (i.e. Secure File Transfer over SSH),
follows the same concepts as typical file I/O programming -- i.e. open a file,
read, write, close, etc.).

// Assume at this point we already have a Chilkat.SFtp object
// connected to a server, authenticated, and InitializeSftp has
// already been called..


// To upload a binary file from Byte[], the procedure is to open the remote file,
// write the data, and then close the file.

// Open a remote file, returning a handle to the open file.
string handle = sftp.OpenFile("test.dat", "readWrite", "createTruncate");
if (!sftp.LastMethodSuccess)
    {
    textBox2.Text = sftp.LastErrorText;
    return;
    }

string text = "To live is the rarest thing in the world. Most people exist, that is all. -- Oscar Wilde\r\n";
byte[] bytes= System.Text.Encoding.UTF8.GetBytes(text);

// Write some data to the file.
bool success;
for (int i = 0; i < 20; i++)
    {
    // It is possible to write bytes:
    success = sftp.WriteFileBytes(handle, bytes);
    // It is also possible to write the string:
    if (success) success = sftp.WriteFileText(handle, "utf-8", text);

    if (!success)
        {
        textBox2.Text = sftp.LastErrorText;
        return;
        }
    }

// Close the remote file.
if (!sftp.CloseHandle(handle))
    {
    textBox2.Text = sftp.LastErrorText;
    return;
    }
 

SFTP Permission Denied on OpenFile

Question:
I’m getting error after sftp.OpenFile() call

  OpenFile:
    DllDate: Nov 27 2008
    UnlockPrefix: Anything for 30-day trial
    Username: chilkat
    Component: .NET 2.0
    SshVersion: SSH-2.0-5.3.3.56 SSH Tectia Server
    SftpVersion: 3
    hcCurDate: Fri, 28 Nov 2008 09:53:46 +0200
    hcExpire: 1/2009
    filename: sftp_test.txt
    access: writeOnly
    createDisposition: openOrCreate
    v3Flags: 0xa
    Sent FXP_OPEN
    [SSH] Received IGNORE message
    StatusCode: 3
    ErrorMessage: Permission denied, file: sftp_test.txt
    SshLog: 
SFTP> Sending SSH_FXP_OPEN

Answer:

The “Permission denied, file: sftp_test.txt” message is what comes back
from the SFTP server. The filename has no path part, so you’re trying to
open/create a file in the home directory of the SFTP user account used in the authentication.
(AuthenticatePw method I assume?)

Try providing an absolute filepath instead. Also, perhaps try “readWrite” instead
of “writeOnly”.