Question

I am trying to upload a file to a Server using sftp. I have downloaded and installed Chilkat and i am downloading files without any issues. But when i try to upload files to the server, i get no error stating that the uploading files. When i check for response messages, it says "file upload success 1" and one is true But the files doesn't get uploaded to the server.

this is my code:

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

Can anyone help me find out what I am doing wrong?

Was it helpful?

Solution

Found the Issue, in the upload method i had handle variable instead of the ftp variable.

here is the solution:

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
                }
            }
        }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top