(SQL Server) Get Contents of File as Base64
Demonstrates how to read the contents of a file and convert to a base64 string.
-- 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 @bd int
-- Use "Chilkat_9_5_0.BinData" for versions of Chilkat < 10.0.0
EXEC @hr = sp_OACreate 'Chilkat.BinData', @bd OUT
IF @hr <> 0
BEGIN
PRINT 'Failed to create ActiveX component'
RETURN
END
DECLARE @success int
EXEC sp_OAMethod @bd, 'LoadFile', @success OUT, 'qa_data/jpg/starfish.jpg'
IF @success = 0
BEGIN
PRINT 'Failed to load file.'
EXEC @hr = sp_OADestroy @bd
RETURN
END
EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'base64'
PRINT @sTmp0
-- If you want mult-line base64:
PRINT '--'
EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'base64_mime'
PRINT @sTmp0
-- If you want hex..
PRINT '--'
EXEC sp_OAMethod @bd, 'GetEncoded', @sTmp0 OUT, 'hex'
PRINT @sTmp0
-- etc.
EXEC @hr = sp_OADestroy @bd
END
GO
|