1

我正在尝试使用 sftp 将文件上传到服务器。我已经下载并安装了 Chilkat,我正在下载文件没有任何问题。但是当我尝试将文件上传到服务器时,我没有收到错误说明正在上传文件。当我检查响应消息时,它显示“文件上传成功 1”,其中一个为,但文件没有上传到服务器。

这是我的代码:

 public void UploadAndMoveFile()
        {
            bool success = false;
            string path = @"\\geodis\";
            string archive = @"\\Archive\";
            string[] files = Directory.GetFiles(path);
            if (files.Count() == 0)
            {
//no files
            }

            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string fileSource = path + fileName;
                string fileDestination = archive + fileName;
                string handle;
                string ftp = @"\IN\"+fileName;
                handle = sftp.OpenFile(ftp, "writeOnly", "createTruncate");
                if (handle == null)
                {
                    Console.WriteLine(sftp.LastErrorText);
                    return;
                }
                success = sftp.UploadFile(handle, fileSource);
                if (success == true)
                {
                    AppendLogFile("Uploading File Succeeded", "Uploade File", fileName);
                    System.IO.File.Move(fileSource, fileDestination);
                    AppendLogFile("Moving File Succeeded", "Moving File", fileName);
                }
                else
                {
                   // no files
                }
            }
        }

谁能帮我找出我做错了什么?

4

1 回答 1

1

发现问题,在上传方法中我有句柄变量而不是 ftp 变量。

这是解决方案:

public void UploadAndMoveFile()
        {
            bool success = false;
            string path = @"\\geodis\";
            string archive = @"\\Archive\";
            string[] files = Directory.GetFiles(path);
            if (files.Count() == 0)
            {
//no files
            }

            foreach (string file in files)
            {
                string fileName = Path.GetFileName(file);
                string fileSource = path + fileName;
                string fileDestination = archive + fileName;
                string handle;
                string ftp = @"\IN\"+fileName;
                handle = sftp.OpenFile(ftp, "writeOnly", "createTruncate");
                if (handle == null)
                {
                    Console.WriteLine(sftp.LastErrorText);
                    return;
                }
                success = sftp.UploadFile(ftp, fileSource);
                if (success == true)
                {
                    AppendLogFile("Uploading File Succeeded", "Uploade File", fileName);
                    System.IO.File.Move(fileSource, fileDestination);
                    AppendLogFile("Moving File Succeeded", "Moving File", fileName);
                }
                else
                {
                   // no files
                }
            }
        }
于 2013-02-14T13:44:02.673 回答