This project is read-only.

File upload failing - need help

Nov 11, 2014 at 9:17 PM
Edited Nov 11, 2014 at 9:17 PM
I am currently trying to upload a file via sFTP:
        public bool UploadFile(Settings settings, string localPath)
        {
            try
            {
                using (var sftp = new SftpClient(settings.Host, settings.Username, settings.Password))
                {
                    sftp.Connect();
                    using (var fileStream = File.OpenRead(localPath))
                    {
                        sftp.UploadFile(fileStream, settings.RemoteDirectory, true);
                    }
                    sftp.Disconnect();
                }
                return true;
            }
            catch (Exception ex)
            {
                Utility.Err(ex, "UploadFile()");
                return false;
            }
        }
The error message I am getting is:
Renci.SshNet.Common.SshException was caught
  HResult=-2146233088
  Message=Failure
  Source=Renci.SshNet
  StackTrace:
       at Renci.SshNet.Sftp.SftpSession.RequestOpen(String path, Flags flags, Boolean nullOnError)
       at Renci.SshNet.SftpClient.InternalUploadFile(Stream input, String path, Flags flags, SftpUploadAsyncResult asyncResult, Action`1 uploadCallback)
       at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Boolean canOverride, Action`1 uploadCallback)
       at Renci.SshNet.SftpClient.UploadFile(Stream input, String path, Action`1 uploadCallback)
       at MyProj.Models.MyClass.UploadFile(Settings settings, String localPath) in c:\Users\Me\Documents\Visual Studio 2013\Projects\MyProj\MyProj\Models\MyClass.cs:line 19
  InnerException: 
There is no inner exception given

The remote directory I am trying to upload to is "./inbound/". When downloading, using "./outbound/" works fine.

Any help will be appreciated
Nov 11, 2014 at 9:34 PM
Edited Nov 11, 2014 at 9:35 PM
The issue was I needed to append the intended file name on the remote directory like so:
public bool UploadFile(Settings settings, string localPath, string fileName)
        {
            try
            {
                using (var sftp = new SftpClient(settings.Host, settings.Username, settings.Password))
                {
                    sftp.Connect();
                    using (var fileStream = File.OpenRead(localPath))
                    {
                        var remotePath = settings.RemoteDirectory + fileName;
                        sftp.UploadFile(fileStream, remotePath, true);
                    }
                    sftp.Disconnect();
                }
                return true;
            }
            catch (Exception ex)
            {
                Utility.Err(ex, "UploadFile()");
                return false;
            }
        }
Now i just pass in the filename, and append it to the remote directory

The same is true for renaming files, both strings will need the full path and file name:
        public bool RenameRemoteFile(Settings settings, string oldFilePath, string newFilePath)
        {
            try
            {
                using (var sftp = new SftpClient(settings.Host, settings.Username, settings.Password))
                {
                    sftp.Connect();
                    sftp.RenameFile(oldFilePath, newFilePath);
                    sftp.Disconnect();
                }
                return true;
            }
            catch (Exception ex)
            {
                Utility.Err(ex, "RenameRemoteFile()");
                return false;
            }
        }
Jan 20, 2015 at 5:23 PM
Hi

Any solution to this Issue?


I have the same problem Please somebody Help Me!!!!!

Thanks
Jan 20, 2015 at 5:36 PM
The Solution is in the second comment of this discussion.
You have to specify the full path.
Jan 20, 2015 at 5:37 PM
Where????
    static void Main(string[] args)
    {
        string host = "*.*.*.*";
        string username = "test";
        string password = "********";



        string localFileName = "C://TEST.CSV";
        string remoteFileName = System.IO.Path.GetFileName("t.CSV");



        Console.ReadLine();

        SshClient sshClient = new SshClient(host, 22, username, password);
        sshClient.Connect();
        using (var sftp = new SftpClient(sshClient.ConnectionInfo))
        {

            sftp.Connect();

            using (var file = File.OpenRead(localFileName))
            {

                sftp.UploadFile(file, remoteFileName);
            }

            sftp.Disconnect();
        }
    }
Jan 20, 2015 at 5:43 PM
I tried to put like this

string remoteFileName = System.IO.Path.GetFileName("sftp://testdev@10.2.11.5/testdev/Conversion/T.CSV"); but its doesn't work neither....

PLEASE HELP ME
Jan 21, 2015 at 7:11 AM
Edited Jan 21, 2015 at 7:30 AM
jorgefdez205 wrote:
Where????
    static void Main(string[] args)
    {
        string host = "*.*.*.*";
        string username = "test";
        string password = "********";



        string localFileName = "C://TEST.CSV";
        string remoteFileName = System.IO.Path.GetFileName("t.CSV");



        Console.ReadLine();

        SshClient sshClient = new SshClient(host, 22, username, password);
        sshClient.Connect();
        using (var sftp = new SftpClient(sshClient.ConnectionInfo))
        {

            sftp.Connect();

            using (var file = File.OpenRead(localFileName))
            {

                sftp.UploadFile(file, remoteFileName);
            }

            sftp.Disconnect();
        }
    }
Please paste your exception. I highly doubt you really have the same problem.
string remoteFileName = System.IO.Path.GetFileName("sftp://testdev@10.2.11.5/testdev/Conversion/T.CSV"); 
string remoteFileName = System.IO.Path.GetFileName("t.CSV");
Why do you want to use Path.GetFileName?
Both just return "T.CSV". So your file should be uploaded to "/home/test/T.CSV".

Examples how to use UploadFile, can be found in the tests:
https://sshnet.codeplex.com/SourceControl/latest#Renci.SshClient/Renci.SshNet.Tests/Classes/SftpClientTest.Upload.cs
Jan 21, 2015 at 4:49 PM
Edited Jan 21, 2015 at 4:49 PM
THANK YOU SO MUCH !!!!!! Is now working, I just changed string remoteFileName = System.IO.Path.GetFileName("t.CSV"); par string remoteFileName = "./t.CSV";
thank you again da_rinkes