Page 1 of 1

Help with simple file upload sequence

Posted: Wed 01 Dec 2010 06:55
by ScottG
Hi,
I think this is just what I need to do a simple file upload to an SSH server.

I can get it all working with each event driven from buttons but I have some problems with the sequence and events to make it automatic.

My program needs to connect, upload a file, and disconnect, that's all.

Basically I do this, each fired from a form button and it works great:
ScSSHClient1.Connect;
ScSFTPClient1.Initialize;
ScSFTPClient1.UploadFile();
ScSFTPClient1.Disconnect;
ScSSHClient1.Disconnect;

To automate this I use the ScSSHClient1.AfterConnect event to fire the ScSFTPClient1.Initialize;

I was hoping to use the ScSFTPClient1.OnConnect to fire the UploadFile but here is where I think I an running into trouble, it is not AfterConnect, so within this OnConnect event I cant do anything as I assume I am not yet connected.

Also when uploading I know I can look for the second OnSuccess to disconnect but I dont want to use that because I want to be able to abort and disconnect everything if (for example) the upload file is not ready or has errors etc. So I can't rely on the OnSucces event to close it all down.

If I abort and try to disconnect the SSH from within the SFTP.OnConnect I get stuck in a loop.

I assume the SSH reconnectes when the SFTP.OnConnect event ends and that retriggers the SSH.AfterConnect, which in turn executes my SFTP.Initilize again and so on.

Any ideas how I can get this sequence to fire automatically and also have the option to abort the upload and disconnect if required?

BR

Posted: Thu 02 Dec 2010 04:35
by ScottG
I just wanted to add something more,

I have started to work around some issues by trying to wait for SFTP to connect before either starting a file upload or disconnecting.

I have run into another problem.
The SFTP has a TimeOut setting but I am not sure how I can tell if the connection completed or timed-out.

There is not an OnTimeout event and there is not a Connected property, how can I tell when the SFTP connection is complete?

The same goes for SSH, there is a timeout setting but not an OnTimeout event, but there is a 'Connected' property I can poll after my own timer.

BR

Re: Help with simple file upload sequence

Posted: Fri 03 Dec 2010 10:01
by Dimon
ScottG wrote:Basically I do this, each fired from a form button and it works great:
ScSSHClient1.Connect;
ScSFTPClient1.Initialize;
ScSFTPClient1.UploadFile();
ScSFTPClient1.Disconnect;
ScSSHClient1.Disconnect;

To automate this I use the ScSSHClient1.AfterConnect event to fire the ScSFTPClient1.Initialize;

I was hoping to use the ScSFTPClient1.OnConnect to fire the UploadFile but here is where I think I an running into trouble, it is not AfterConnect, so within this OnConnect event I cant do anything as I assume I am not yet connected.

Also when uploading I know I can look for the second OnSuccess to disconnect but I dont want to use that because I want to be able to abort and disconnect everything if (for example) the upload file is not ready or has errors etc. So I can't rely on the OnSucces event to close it all down.
Why aren't you able to execute these methods one-by-one in one form button event without processing any SSH/SFTP events?

Posted: Fri 03 Dec 2010 10:04
by Dimon
ScottG wrote:The SFTP has a TimeOut setting but I am not sure how I can tell if the connection completed or timed-out.

There is not an OnTimeout event and there is not a Connected property, how can I tell when the SFTP connection is complete?

The same goes for SSH, there is a timeout setting but not an OnTimeout event, but there is a 'Connected' property I can poll after my own timer.
You can use the TScSFTPClient.Active property to determine whether the connection to SFTP server is established. If on establishing SFTP connection an error is arised, then the Initialize method raises this error and you can catch it in the try..except block.

If on establishing SFTP connection time runs out, then the Initialize method raises an exception.

Posted: Sat 04 Dec 2010 00:47
by ScottG
Perfect, thanks

Having the Initialize method raise an exception on timeout is just what I need.

My application runs on a PC with no keyboard so I need to cover all bases, a 'click OK to continue' means a phone call to the IT dept, me :-(

BR

Posted: Mon 06 Dec 2010 09:11
by Dimon
To avoid dispalying the error messages you can catch errors in the try..except block. For example:

Code: Select all

  try
    ScSFTPClient1.Initialize;
  except
    on E: EScError do
      ...
  end;