Need Help With SFTP UploadFile

Discussion of open issues, suggestions and bugs regarding network security and data protection solution - SecureBridge
Post Reply
Wood1233
Posts: 4
Joined: Mon 10 Sep 2012 14:48

Need Help With SFTP UploadFile

Post by Wood1233 » Wed 12 Sep 2012 18:02

I am new to dealing with sftp. I found a couple of helpful forums and came up with this code to upload a file to an sftp site. I dropped on the TScSSHClient and TScSFTPClient components, naming them respectively to SSHClient and SFTPClient and then call this code from another form that sends in the host, user, password and file to be uploaded. I know I am getting a connection because the function; SFTPClient->DownloadFile("/TransferTest.txt", "C:\\TransferTest.txt", true );
works great and I pull the file right to my C drive. It is only on the upload that I recieve the error in my catch "Cannot send data to server". This is pretty vague so I don't know what part is not right? Can someone tell me if they see anything in this code that could be causing this error?

This was built on RAD Studio XE2

Code: Select all

bool TfrmFTPSend::SendSFTPFile(UnicodeString host, UnicodeString user, UnicodeString pass, UnicodeString locFile)
{
   bool successful = true;

   TScFileStorage *KeyFileStorage = new TScFileStorage(this);
   SSHClient->KeyStorage = KeyFileStorage;
   SSHClient->Authentication = atPassword;
   SSHClient->Port = 22;
   SSHClient->HostName = host;
   SSHClient->User = user;
   SSHClient->Password = pass;

   SFTPClient->SSHClient = SSHClient;

   try
      {
         // Initialize session
         SSHClient->Connect();
         SFTPClient->Initialize();

         // send file
         SFTPClient->UploadFile(locFile, L"", false);
      }
   catch (Exception &E)
      {
         ErrMsg(ETYPE_ERROR, 0, E.Message, "", "");
         successful = false;
      }
   catch (...)
      {
         successful = false;
      }

   // Finish/cleanup
   SFTPClient->Disconnect();
   SSHClient->Disconnect();
   delete SFTPClient;
   delete SSHClient;
   delete KeyFileStorage;

   return successful;
}

Wood1233
Posts: 4
Joined: Mon 10 Sep 2012 14:48

Re: Need Help With SFTP UploadFile

Post by Wood1233 » Wed 12 Sep 2012 18:11

I knew this would happen as soon as I posted this question, because that is just the way it works. I found the answer to my own question.

Changing the Upload call to this fixed it. I was not naming the outgoing file to anything at all.

Code: Select all

SFTPClient->UploadFile(locFile, L"/" + ExtractFileName(locFile), false);

AndreyZ

Re: Need Help With SFTP UploadFile

Post by AndreyZ » Fri 14 Sep 2012 13:57

It's good to see that you've found a solution. If any other questions come up, please contact us.

Post Reply