SQL server to convert base64 data into a file

Deadly 提交于 2020-01-25 14:26:29

问题


Scenario :

  • I convert a pdf file to base64 and put it in an xml, and

Problem :

  • when I receive the xml I will have to convert this base64 data back to the original pdf file using SQL Server and store it somewhere.

I found this link but could not figure out how to do it.
This is what I have:

DECLARE 
    @SQLcommand VARCHAR(8000),
    @MyOriginalFile VARCHAR(8000),
    @RawData VARCHAR(8000)

SET @RawData = 'JVBERi0x etc'

SET @SQLcommand = 'bcp "SELECT @MyOriginalFile = @RawData" queryout "\\MY-SERVER\MySharedFolder\New.pdf" -T -n -S A70195\dev'

EXEC xp_cmdshell @SQLcommand
  • I think I need to understand what does the -T -n -S A70195\dev mean.

    Can someone please help?
    Thanks.

回答1:


Starting from SQL2005 I would use VARBINARY(MAX) data type for PDF content and FOR XML ... , BINARY BASE 64 to convert binary values to BASE64:

-- VarBinary -> XML
DECLARE @PdfContent VARBINARY(MAX)
SET @PdfContent = 0x12345678

SELECT  @PdfContent AS BinaryContent
FOR XML RAW, BINARY BASE64
-- Output <row BinaryContent="EjRWeA==" />

-- XML -> VarBinary
DECLARE @PdfContentAsXML XML
SET     @PdfContentAsXML = N'<row BinaryContent="EjRWeA==" />'
SELECT  x.XmlCol.value('(@BinaryContent)', 'VARBINARY(MAX)') AS BinaryContentFromXml
FROM    @PdfContentAsXML.nodes('row') x(XmlCol)
-- Output 0x12345678


来源:https://stackoverflow.com/questions/29313962/sql-server-to-convert-base64-data-into-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!