public static void SendFileToFtps([Ftp] out FtpMessage ftpMessage) { var stream = new MemoryStream(Encoding.UTF8.GetBytes("Nu skickar vi en fil!")); ftpMessage = new FtpMessage { Filename = "/tmp/testfile.txt", Data = stream }; }
public async Task SendFileAsync(FtpMessage ftpMessage) { if (ftpMessage.Send) { switch (ftpMessage.Configuration.FtpHost.Scheme) { case "sftp": await SendBySftp(ftpMessage); break; case "ftps": await SendByFtps(ftpMessage); break; default: throw new ArgumentException("Unsupported uri scheme. Only ftps and sftp is supported.", nameof(ftpMessage)); } } }
private async Task SendByFtps(FtpMessage ftpMessage) { var host = ftpMessage.Configuration.FtpHost; var path = ftpMessage.Filename; var username = ftpMessage.Configuration.Username; var password = ftpMessage.Configuration.Password; var port = ftpMessage.Configuration.FtpPort; using (var client = new FTPSClient()) { client.Connect(host.Host, new NetworkCredential(username, password), ESSLSupportMode.CredentialsRequired | ESSLSupportMode.DataChannelRequested); var ftps = client.PutFile(path); await ftps.WriteAsync(ftpMessage.Data, 0, ftpMessage.Data.Length); ftps.Close(); } }
private async Task SendBySftp(FtpMessage ftpMessage) { var host = ftpMessage.Configuration.FtpHost; var path = ftpMessage.Filename; var username = ftpMessage.Configuration.Username; var password = ftpMessage.Configuration.Password; var port = ftpMessage.Configuration.FtpPort; using (var sftpClient = new SftpClient(host.Host, port, username, password)) { sftpClient.Connect(); //sftpClient.ChangeDirectory("tmp"); var ms = new MemoryStream(ftpMessage.Data); sftpClient.UploadFile(ms, path); sftpClient.Disconnect(); } }