SQL Server
SQL Server
Download Web Page to MHT and Extract Images (all in memory)
See more MHT / HTML Email Examples
Downloads a web page to an MHT archive (in memory) and then extracts each image to a byte array in memory.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
DECLARE @iTmp0 int
DECLARE @sTmp0 nvarchar(4000)
DECLARE @success int
SELECT @success = 0
-- This example assumes the Chilkat API to have been previously unlocked.
-- See Global Unlock Sample for sample code.
-- Note: This URL exists at the time of writing and testing this example (on 12-June-2020)
-- However, it will surely not continue to exist for very long.
-- You should choose a different URL. (Any web page with images will do.)
DECLARE @url nvarchar(4000)
SELECT @url = 'https://www.fendi.com/it/abbigliamento-uomo/cravatta-fxc160a3nwf0qg2'
DECLARE @mht int
EXEC @hr = sp_OACreate 'Chilkat.Mht', @mht OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
-- Downloads to an MHT string.
-- MHT is just MIME, which is the same format as an email but with different semantics.
DECLARE @mhtStr nvarchar(4000)
EXEC sp_OAMethod @mht, 'GetMHT', @mhtStr OUT, @url
EXEC sp_OAGetProperty @mht, 'LastMethodSuccess', @iTmp0 OUT
IF @iTmp0 = 0
BEGIN
EXEC sp_OAGetProperty @mht, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mht
RETURN
END
-- We can still treat the MHT MIME as an email and iterate over the "related items".
DECLARE @email int
EXEC @hr = sp_OACreate 'Chilkat.Email', @email OUT
EXEC sp_OAMethod @email, 'SetFromMimeText', @success OUT, @mhtStr
IF @success = 0
BEGIN
EXEC sp_OAGetProperty @email, 'LastErrorText', @sTmp0 OUT
PRINT @sTmp0
EXEC @hr = sp_OADestroy @mht
EXEC @hr = sp_OADestroy @email
RETURN
END
DECLARE @numRelatedItems int
EXEC sp_OAGetProperty @email, 'NumRelatedItems', @numRelatedItems OUT
DECLARE @i int
SELECT @i = 0
DECLARE @sbContentType int
EXEC @hr = sp_OACreate 'Chilkat.StringBuilder', @sbContentType OUT
WHILE @i < @numRelatedItems
BEGIN
EXEC sp_OAMethod @email, 'GetRelatedContentType', @sTmp0 OUT, @i
EXEC sp_OAMethod @sbContentType, 'SetString', @success OUT, @sTmp0
EXEC sp_OAMethod @sbContentType, 'GetAsString', @sTmp0 OUT
PRINT 'Content-Type: ' + @sTmp0
EXEC sp_OAMethod @sbContentType, 'StartsWith', @iTmp0 OUT, 'image/', 0
IF @iTmp0 = 1
BEGIN
-- We have an image.
-- Get the image data.
ERROR: Cannot use pbytes in SQL Server examples...
EXEC sp_OAMethod @email, 'GetRelatedData', @imageData OUT, @i
-- Do what you need with the image data..
END
SELECT @i = @i + 1
END
EXEC @hr = sp_OADestroy @mht
EXEC @hr = sp_OADestroy @email
EXEC @hr = sp_OADestroy @sbContentType
END
GO