【问题标题】:Download a p7m file from a SQL Server database从 SQL Server 数据库下载 p7m 文件
【发布时间】:2025-12-21 01:25:17
【问题描述】:

我有以下问题,我在 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】:

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

    此解决方案有效

     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 
    
    
    

    【讨论】: