从 SQL 服务器数据库下载一个 p7m 文件

Download a p7m file from a SQL Server database

我有以下问题,我将 p7m 文件插入到 SQL Server 2005 的 varbinary(max) 列中。

当我从经典 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中插入。

因为下面的流剪切了最后一个字符,我添加了一个空字符作为二进制

这个解决方案有效

 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