从SQL Server数据库下载p7m文件

问题描述 投票:0回答:1

我有以下问题,我在SQL Server 2005的varbinary(max)列中插入了一个p7m文件。

当从经典的ASP页面中尝试使用以下vbscript代码下载它时:

Dim sSql, oCmd, nomeric
Set oCmd = server.CreateObject("ADODB.Command")
Set oCmd.ActiveConnection = OBJdbConnection
oCmd.CommandType = adCmdText

sSql = "SELECT fileP7m FROM tabella "

Response.ContentType = "application/pkcs7-mime"
Response.AddHeader "Content-Disposition", "attachment; filename=" & nomeric

oCmd.CommandText = sSql

oCmd.Properties("Output Stream").Value = Response
oCmd.Execute , , 1024

Set oCmd = nothing

该文件已正确下载,可以正常使用Adobe Acrobat打开它,但是如果我将该文件提交到站点以验证数字签名,则它会失败,而原始文件会运行良好。

如果我打开用notepad ++下载的文件,并且最初的文件第一个包含一些非常奇怪的字符,例如中文或印度语,我真的不知道。使我以这种方式转换文件p7m的错误在哪里?为什么不可能验证数字标牌?

谢谢最好的问候。

为了完整起见,我还写了将文件插入数据库的方式。pContent是包含整个文件p7m的字符串。

Dim currFileStream, oCmd, i
SET oCmd = server.CreateObject ("ADODB.Command")
SET currFileStream = server.CreateObject ("ADODB.Stream")

currFileStream.Type = 2
currFileStream.Open

For i = 1 To Len(pContent)
    currFileStream.WriteText ChrB(Asc(Mid(pContent, i, 1)))
Next

oCmd("@fileAttached").AppendChunk currFileStream.Read(currFileStream.Size)
oCmd.Parameters.Append oCmd.CreateParameter("@fileAllegato", adLongVarBinary, adParamInput, currFileStream.Size)

存储过程在fileAttached列中运行插入。

sql-server-2005 vbscript
1个回答
0
投票

由于以下流削减了最后一个字符,因此我将一个空字符添加为二进制字符

此解决方案有效

sSql =  "SELECT fileAllegato + CAST(' ' AS varbinary(1)) fileAllegato " & _
                "FROM Table"

        oCmd.CommandText = sSql
        set rc_file = oCmd.Execute
        if rc_file.EOF then 
            set oCmd = nothing
            Response.Write ("Allegato 
            non trovato.")
        else 
            fileall = rc_file("fileAllegato")
        end if  

        rc_file.close
        set rc_file = nothing

        Const adTypeText = 2
        Const adTypeBinary = 1

        Dim BinaryStream
        Set BinaryStream = CreateObject("ADODB.Stream")

        BinaryStream.Type = adTypeBinary
        BinaryStream.Open
        BinaryStream.Write fileall 

        BinaryStream.Position = 0
        BinaryStream.Type = adTypeText

        Dim BinaryToString:BinaryToString = BinaryStream.ReadText(-1) 

        BinaryStream.Close
        Set BinaryStream = Nothing

        Response.ContentType = "application/pkcs7-mime"
        Response.AddHeader "Content-Disposition", "attachment; filename=" & nomefile
        Response.BinaryWrite BinaryToString 
© www.soinside.com 2019 - 2024. All rights reserved.