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;
}
admin
0
Tags :