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
Data Binding in Wpf User Controls
How to Use Enumwindows to Find Windows with a Specific Caption/Title
Writing Recursive Cte Using Entity Framework Fluent Syntax or Inline Syntax
How to Cast String to Int. Error Msg: Input String Was Not in a Correct Format
Swagger UI Web API Documentation Present Enums as Strings
Validation: How to Inject a Model State Wrapper with Ninject
What Are the Definitions for Lparam and Wparam
Icollection<T> VS List<T> in Entity Framework
How Would You Code an Efficient Circular Buffer in Java or C#
Use Linq to Group a Sequence of Numbers with No Gaps
How to Put a Task to Sleep (Or Delay) in C# 4.0
How to Convert a Utf-8 String into Unicode
Error on If Statement - Cannot Implicitly Convert Type to 'Bool'
How to Upload Files in ASP.NET Core
Passing a Vector/Array from Unmanaged C++ to C#
What Setup Code Should Go in Form Constructors Versus Form Load Event