Upload to FTP server C# using Tamir.SharpSSH

心已入冬 提交于 2019-12-10 11:34:30

问题


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.


回答1:


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 ..\...\.




回答2:


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;
}


来源:https://stackoverflow.com/questions/25178410/upload-to-ftp-server-c-sharp-using-tamir-sharpssh

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!