How to Upload a File to an Sftp Server in C# (.Net)

C# SFTP upload files

What you are trying to do here is to establish a FTPS connection which is not a SFTP connection. The EnableSsl option only activates FTP over TLS (so FTPS). It uses Port 21 to connect to the server.

If you really have activated SFTP in FileZilla, you have to use an SSH connection on port 22 to connect to the server (SFTP = SSH File Transfer Protocol). The easiest method to obtain this should be using SharpSSH.

You can also take a look into this question.

How to upload file in server using SFTP through ASP.NET?

The sftpHost argument of Tamir.SharpSsh.Sftp constructor takes an IP address or a host name, not a URL. And even if it took a URL, it would be sftp://, not ftp://.

So it should be:

Sftp oSftp = new Sftp("22.00.333.444", _UserName, _Password);

But of course 22.00.333.444 is not a valid IP address.


And I strongly discourage you from using SharpSSH. It's a dead project.

For alternatives, see:

  • How do I upload a file to an SFTP server in C# / .NET?
  • SFTP Libraries for .NET

Upload data from memory to SFTP server using SSH.NET

Wrap the byte array to a MemoryStream and use SftpClient.UploadFile to upload it

var client = new SftpClient(host, port, username, password);
client.Connect();
var stream = new MemoryStream(results);
client.UploadFile(stream, "/remote/path/my.pdf");

Though most data-manipulation libraries can use the Stream API directly. That would be more efficient to use, instead of copying the data over via byte[] array.

Is it possible to upload a CSV file to an SFTP server directly from a MemoryStream?

It looks like the csvFileWriter.Write already returns MemoryStream. And its ToString returns "System.IO.MemoryStream" string. That's the root source of your problem.

Aditionally, as you already have the MemoryStream, its an overkill to copy it to yet another MemoryStream, upload it directly. You are copying the data over and over again, it's just a waste of memory.

Like this:

var stream = csvFileWriter.Write(data, new CsvMapper());
stream.Position = 0;
client.UploadFile(stream, destination);

See also:

  • Upload data from memory to SFTP server using SSH.NET
  • When uploading memory stream with contents created by csvhelper using SSH.NET to SFTP server, the uploaded file is empty

A simple test code to upload in-memory data:

var stream = new MemoryStream();
stream.Write(Encoding.UTF8.GetBytes("this is test"));
stream.Position = 0;

using (var client = new SftpClient("example.com", "username", "password"))
{
client.Connect();
client.UploadFile(stream, "/remote/path/file.txt");
}

Upload file uploaded via HTTP to ASP.NET further to SFTP server in C# and SSH.NET

Your immediate problem is answered here:

Upload from ByteArray/MemoryStream using SSH.NET - File gets created with size 0KB


Though you do not need the intermediate MemoryStream (and it's inefficient anyway).

Use IFormFile.OpenReadStream:

using (var uplfileStream = formFile.OpenReadStream())
{
sftp.UploadFile(uplfileStream, Name);
}

Upload file to SFTP server with different extension

The remotePath argument of the Session.PutFiles method is:

Full path to upload the file to.

So, all you need to do, is to specify the full path like:

/remote/path/temp.csv.ready


Related Topics



Leave a reply



Submit