从 SQL Server 数据库下载 p7m 文件



我有以下问题,我在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正常打开它,但是如果我将文件提交到站点以验证数字签名,则会失败,而原始文件运行良好。

如果我打开用记事本++下载的文件,第一个文件包含一些非常奇怪的字符,比如中文或印度文,我真的不知道。使我以这种方式转换文件 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 

最新更新