文件未从 Windows 传输到 Linux 远程服务器

Files not getting transferred from Windows to Linux remote server

我正在尝试在 visual studio 中使用 WinSCP。我使用 Managed NuGet 包下载并安装了 WinSCP。我在 Web 应用程序中使用以下代码将其中一个文件传输到远程 Linux 服务器。代码执行正常,没有任何错误,但文件未传输。我使用 PuTTY 登录以验证文件是否已实际传输,但找不到文件。下面是使用的代码

public int Upload(String HostName, String UserName, String Password, String remotePath, String localFilePath)
{
    int result = 0;
    Session session = null;
    try
    {
        // Setup session options               
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Ftp,
            HostName = HostName,
            UserName = UserName,
            Password = Password,
            Timeout = TimeSpan.FromDays(1),

        };

        using (session = new Session())
        {
            // Connect
            session.Open(sessionOptions);

            // upload files
            TransferOptions transferOptions = new TransferOptions();
            transferOptions.TransferMode = TransferMode.Ascii;

            TransferOperationResult transferResult = null;
            transferResult = session.PutFiles(localFilePath, remotePath, false, transferOptions);

            //  Throw on any error
            transferResult.Check();
            //  Print results
            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.WriteLine("Upload of {0} succeeded", transfer.FileName);
            }
            session.GetFiles(@"\remoteserver\folder1\folder_backups\test_files\test1.txt", @"d:\folder3\").Check();
        }

        result = 0;
    }
    catch (Exception e)
    {
        Console.WriteLine("Error: {0}", e);
        result = 1;
    }
    finally
    {
        if (session != null)
        {
            session.Dispose();
        }
    }
    return result;
}

传递的参数如下:

project1.Upload("remote host server", "username", "password", @"\remote host server\folder1\folder_backups\test_files\", Fileupload1.PostedFile.FileName);

代码执行没有任何错误,但没有上传或下载文件。如何解决这个问题? 谢谢

不完全确定,但看起来您已将协议设置为 FTP,服务器可能不支持该协议。如果您能够通过 putty 登录,则意味着 SSH 连接是可能的。尝试将协议设置为 SFTP.

After the login happens in GUI - it points to /home/UserId . But the folder which i want to move the files exist in /folder1

如果您要使用的远程路径是 /folder1/,请将其用于 Upload 方法的 remotePath 参数,而不是明显错误的值 @"\remote host server\folder1\folder_backups\test_files\".

project1.Upload("host", "user", "password", "/folder1/", Fileupload1.PostedFile.FileName);