SQL Server
SQL Server
HTTP Download any Type of File (binary or text)
See more HTTP Examples
TheDownload method can download any file type, whether binary (e.g., .zip, .pdf) or text (.xml, .txt), without distinction. It streams the file byte-for-byte from the web server exactly as received. This same process applies to web pages: providing a URL typically viewed in a browser downloads the server's delivered HTML to a file.
Chilkat SQL Server Downloads
-- Important: See this note about string length limitations for strings returned by sp_OAMethod calls.
--
CREATE PROCEDURE ChilkatSample
AS
BEGIN
DECLARE @hr int
-- Important: Do not use nvarchar(max). See the warning about using nvarchar(max).
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example requires the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
DECLARE @http int
EXEC @hr = sp_OACreate 'Chilkat.Http', @http OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
EXEC sp_OASetProperty @http, 'KeepResponseBody', 1
-- Download a .zip
DECLARE @localFilePath nvarchar(4000)
SELECT @localFilePath = '/temp/hamlet.zip'
EXEC sp_OAMethod @http, 'Download', @success OUT, 'https://www.chilkatsoft.com/hamlet.zip', @localFilePath
DECLARE @statusCode int
EXEC sp_OAGetProperty @http, 'LastStatus', @statusCode OUT
IF @success = 0
BEGIN
IF @statusCode = 0
BEGIN
-- Unable to either send the request or get the response.
EXEC sp_OAGetProperty @http, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
END
ELSE
BEGIN
-- We got a response, but the status code was not in the 200s
PRINT 'Response status code: ' + @statusCode
-- Examine the response body.
PRINT 'Response body:'
EXEC sp_OAGetProperty @http, 'LastResponseBody', @sTmp0 OUT
PRINT @sTmp0
END
PRINT 'Download failed.'
END
ELSE
BEGIN
PRINT 'Download success, response status = ' + @statusCode
END
-- Download an XML file:
SELECT @localFilePath = '/temp/hamlet.xml'
EXEC sp_OAMethod @http, 'Download', @success OUT, 'https://www.chilkatsoft.com/hamlet.xml', @localFilePath
-- ...
-- Check for errors in the same way as shown above..
-- ...
PRINT 'OK!'
EXEC @hr = sp_OADestroy @http
END
GO