Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(SQL Server) FTP Download Binary File to a StreamDemonstrates how to FTP download a binary file to a Chilkat stream. Note: This example requires Chilkat v9.5.0.67 or greater.
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls. -- CREATE PROCEDURE ChilkatSample AS BEGIN DECLARE @hr int DECLARE @iTmp0 int -- Important: Do not use nvarchar(max). See the warning about using nvarchar(max). DECLARE @sTmp0 nvarchar(4000) -- This example assumes the Chilkat FTP2 API to have been previously unlocked. -- See FTP2 Unlock Sample for sample code. DECLARE @ftp int -- Use "Chilkat_9_5_0.Ftp2" for versions of Chilkat < 10.0.0 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', 'www.my-ftp-server.com' EXEC sp_OASetProperty @ftp, 'Username', 'mFtpLogin' EXEC sp_OASetProperty @ftp, 'Password', 'myFtpPassword' EXEC sp_OASetProperty @ftp, 'AuthTls', 1 EXEC sp_OASetProperty @ftp, 'Passive', 1 -- Connect and login to the FTP server using TLS. DECLARE @success int EXEC sp_OAMethod @ftp, 'Connect', @success OUT IF @success <> 1 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @ftp RETURN END -- We'll read the incoming data chunk-by-chunk and write to an output file. DECLARE @fac int -- Use "Chilkat_9_5_0.FileAccess" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.FileAccess', @fac OUT EXEC sp_OAMethod @fac, 'OpenForWrite', @success OUT, 'qa_output/penguins2.jpg' IF @success <> 1 BEGIN EXEC sp_OAGetProperty @fac, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @ftp EXEC @hr = sp_OADestroy @fac RETURN END DECLARE @streamObj int -- Use "Chilkat_9_5_0.Stream" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.Stream', @streamObj OUT -- Create a task to do the FTP download. DECLARE @t int EXEC sp_OAMethod @ftp, 'GetFileToStreamAsync', @t OUT, 'penguins2.jpg', @streamObj EXEC sp_OAGetProperty @ftp, 'LastMethodSuccess', @iTmp0 OUT IF @iTmp0 <> 1 BEGIN EXEC sp_OAGetProperty @ftp, 'LastErrorText', @sTmp0 OUT PRINT @sTmp0 EXEC @hr = sp_OADestroy @ftp EXEC @hr = sp_OADestroy @fac EXEC @hr = sp_OADestroy @streamObj RETURN END -- Start the task in a background thread. EXEC sp_OAMethod @t, 'Run', @success OUT DECLARE @bd int -- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0 EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT EXEC sp_OAGetProperty @streamObj, 'EndOfStream', @iTmp0 OUT WHILE @iTmp0 <> 1 BEGIN -- Get the next chunk of JPG bytes. EXEC sp_OAMethod @streamObj, 'ReadBd', @success OUT, @bd -- Do something with it.. -- In this simple example, we'll just write to the file. EXEC sp_OAMethod @fac, 'FileWriteBd', @success OUT, @bd, 0, 0 -- Clear for the next read. EXEC sp_OAMethod @bd, 'Clear', @success OUT END EXEC sp_OAMethod @fac, 'FileClose', NULL EXEC @hr = sp_OADestroy @t PRINT 'Success.' EXEC @hr = sp_OADestroy @ftp EXEC @hr = sp_OADestroy @fac EXEC @hr = sp_OADestroy @streamObj EXEC @hr = sp_OADestroy @bd END GO |
© 2000-2025 Chilkat Software, Inc. All Rights Reserved.