Sample code for 30+ languages & platforms
SQL Server

Monitor, Cancel, and Keep-Alive Long FTP Transfers

See more FTP Examples

Demonstrates the HeartbeatMs, AbortCurrent, and LargeFileMeasures properties, which monitor long operations, allow cancellation, and keep the control connection alive during long transfers.

Note: The local paths are relative to the application's current working directory. Absolute paths may also be used. Supply the paths appropriate to your own environment.

Background: A large transfer blocks for a long time, and these properties make that manageable. HeartbeatMs fires a periodic AbortCheck callback so an application can show progress and remain responsive, and AbortCurrent — set from that callback or another thread — cancels the running operation. LargeFileMeasures addresses a subtler problem: while data flows on the data connection, an idle control connection can be dropped by a firewall, so this sends a periodic NOOP to keep it alive.

Chilkat SQL Server Downloads

SQL Server
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
    DECLARE @hr int
    DECLARE @sTmp0 nvarchar(4000)
    DECLARE @success int
    SELECT @success = 0

    DECLARE @ftp int
    EXEC @hr = sp_OACreate 'Chilkat.Ftp2', @ftp OUT
    IF @hr <> 0
    BEGIN
        PRINT 'Failed to create ActiveX component'
        RETURN
    END

    EXEC sp_OASetProperty @ftp, 'Hostname', 'ftp.example.com'
    EXEC sp_OASetProperty @ftp, 'Username', 'myFtpLogin'

    --  Normally you would not hard-code the password in source.  You should instead obtain it
    --  from an interactive prompt, environment variable, or a secrets vault.
    EXEC sp_OASetProperty @ftp, 'Password', 'myPassword'

    --  HeartbeatMs sets how often the AbortCheck event fires during long operations.  The default 0
    --  disables it.  A nonzero value lets an application monitor progress and request cancellation.
    EXEC sp_OASetProperty @ftp, 'HeartbeatMs', 100

    --  AbortCurrent may be set to 1 (typically from another thread, or from an AbortCheck
    --  callback) to cancel a running operation.  Left 0 here so nothing is pre-emptively aborted.
    EXEC sp_OASetProperty @ftp, 'AbortCurrent', 0

    --  LargeFileMeasures (default 0) sends a NOOP on the control channel once per minute during
    --  a long transfer, keeping the control connection alive while data flows.
    EXEC sp_OASetProperty @ftp, 'LargeFileMeasures', 1

    EXEC sp_OAMethod @ftp, 'Connect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    EXEC sp_OAMethod @ftp, 'GetFile', @success OUT, 'public_html/hugefile.iso', 'qa_output/hugefile.iso'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    PRINT 'Transfer complete.'

    EXEC sp_OAMethod @ftp, 'Disconnect', @success OUT
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    EXEC @hr = sp_OADestroy @ftp


END
GO