Sample code for 30+ languages & platforms
SQL Server

Control ASCII Download Line Endings (CrlfMode)

See more FTP Examples

Demonstrates the CrlfMode property, which controls line-ending conversion when downloading in ASCII transfer mode: 0 leaves them as received, 1 converts to CRLF, 2 to LF, and 3 to CR.

Background: When you deliberately download text in ASCII mode, CrlfMode lets you force the saved file's line endings to a specific convention — CRLF for Windows tools, LF for Unix — regardless of what the server sends. It applies only in ASCII mode, so pair it with SetTypeAscii; in the recommended default binary mode, bytes are preserved verbatim and this setting has no effect. Reserve ASCII mode (and this property) for the rare case where line-ending normalization of text is actually wanted.

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'

    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

    --  CrlfMode controls line-ending conversion for ASCII-mode downloads:
    --    0 = leave line endings as received (default)
    --    1 = convert to CRLF
    --    2 = convert to LF
    --    3 = convert to CR
    --  Convert a downloaded text file's line endings to CRLF (useful when saving for Windows).
    EXEC sp_OASetProperty @ftp, 'CrlfMode', 1

    --  CrlfMode applies only in ASCII transfer mode.
    EXEC sp_OAMethod @ftp, 'SetTypeAscii', @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/readme.txt', 'qa_output/readme.txt'
    IF @success = 0
      BEGIN
        EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT
        PRINT @sTmp0
        EXEC @hr = sp_OADestroy @ftp
        RETURN
      END

    PRINT 'Downloaded with CRLF line endings.'

    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