【问题标题】:Problems uploading files using Chilkat使用 Chilkat 上传文件的问题
【发布时间】:2013-02-14 11:25:52
【问题描述】:

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

这是我的代码:

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

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

【问题讨论】:

    标签: c# .net sftp chilkat


    【解决方案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
                    }
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 1970-01-01
      • 2021-09-28
      相关资源
      最近更新 更多