user3868544
user3868544

Reputation:

Upload to FTP server C# using Tamir.SharpSSH

Im able to connect with my sftp server and I'm sure of it because I get the list of files of my server and it passes correct list. But I cant upload file to a folder in mysftp server. Here is my code:

  private static void FileUploadUsingSftp(string SFTPAddress, string SFTPUserName, string SFTPPassword,
        string SFTPFilePath, string FileName)
    {
        Sftp sftp = null;
        try
        {

            sftp = new Sftp( SFTPAddress,SFTPUserName , SFTPPassword);

            // Connect Sftp
            sftp.Connect();
            MessageBox.Show("Connected!");



            //Check if im surely connected
            //list down files in my sftp server folder u01
            ArrayList list;
            list = sftp.GetFileList("//u01");

            foreach (string item in list)
            {
                MessageBox.Show(item.ToString());
            }
            MessageBox.Show(list.Count.ToString());

            // upload file 
            sftp.Put(FileName, "//u01"); -----> **I get exception here**

            MessageBox.Show("UPLOADED!");


            // Close the Sftp connection
            sftp.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
        finally
        {
            if (sftp != null)
            {
                sftp.Close();
            }
        }
    }

I recieve this exception:

"Exception of type 'Tamir.SharpSsh.jsch.SftpException' was thrown."
at Tamir.SharpSsh.jsch.ChannelSftp.put(String src, String dst, SftpProgressMonitormonitor, Int32 mode)
at Tamir.SharpSsh.Sftp.Put(String fromFilePath, String toFilePath)

I've tried using sftp.Put(FileName,SFTPAddress + "//u01");

Ive tried sftp.Put(FileName,SFTPAddress); And it do work but when I look at my sftp server if the file is there, it isn't.

I've tried sftp.Put(FileName,"//u01"); and it throws the same error.

I must upload my file in a folder in my ftp server and one of the folder is u01.

Can anyone help me out. I don't know what's wrong. I'm sure that i'm connected. and when I tried to upload using filezilla it do work so I'm not restricted in writing to our sftp server.

Upvotes: 1

Views: 22922

Answers (2)

Sarath Subramanian
Sarath Subramanian

Reputation: 21341

I was also searching for how to upload a file from local/shared path to SFTP server using this library and finally found the solution. You can use the below code.

string host="ssgty";
string username="usr";
string password="passw";
int port=22;
string fromFile=@"D:\Test.txt";
string toFile=@"/dmon/myfolder/Test.txt";

public string CopyToFTP(string host, string username, string password, int port, string fromFile, string toFile)
{
      string error = "";
      try
      {
           Scp scp = new Scp(host, username, password);
           scp.Connect(port);
           scp.To(fromFile, toFile);
      }
      catch (Exception ex)
      {
           error = ex.Message;
      }
      return error;
}

Upvotes: 0

fredrik
fredrik

Reputation: 6638

I believe that you have to enter the full filename in your call to Put.

string strippedFileName = StripPathComponent(FileName);
sftp.Put(FileName,"//u01//" + strippedFileName);

Note that StripPathComponent is not implemented, you will have to implement that as well if needed. It would strip a path component from FileName, i.e. remove C:\...\ or ..\...\.

Upvotes: 1

Related Questions