ASPNET。签名的PDF文件保存到sql server,检索时出现问题

时间:2013-07-09 09:02:15

标签: asp.net pdf signed

我花了几天时间试图找到这个问题的解决方案,我也尽力搜索网络,我不知道还能做什么,这就是发生的事情:

我已经签署了一份PDF(我不知道这是否相关,但无论如何我都提到了)并且我已将其字节数组保存到varbinary字段内的SQL服务器数据表中。

当我检索数据并使用网络方法File.WriteAllBytes在服务器中创建PDF时,它可以工作,我得到了签名的PDF。

但是,如果我使用任何Response对象的方法来下载该文件或保存在数据库中的字节数组(TransmitFileOutputStream.WriteBinaryWrite),我获取一个空白的PDF文件,其字节内容与原始文件内容不匹配。

我已经尝试了所有这些方法并更改了许多属性(contentTypeAddHeader,...)失败。

我尝试过的代码(你可以看到有很多不同的方法):

arrByte = CType(ds.Tables(0).Rows(0).Item("DATA"), Byte())

'HttpContext.Current.Response.ContentType = "application/pdf"
HttpContext.Current.Response.ContentType = "application/octet-stream"
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=""test.pdf""") 
Dim oStream As New System.IO.MemoryStream(arrByte)
LoadStreamToStream(oStream, Response.OutputStream)

'File.WriteAllBytes(pdfPath, arrByte)
'Dim oFile As FileInfo = New FileInfo(pdfPath)
''If (oFile.Exists) Then
'HttpContext.Current.Response.ClearHeaders()
'HttpContext.Current.Response.Clear()
'HttpContext.Current.Response.ClearContent()
'HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=""test.pdf""")
'HttpContext.Current.Response.Charset = System.Text.Encoding.UTF8.HeaderName
'Response.ContentEncoding = System.Text.Encoding.UTF8
''HttpContext.Current.Response.AddHeader("Content-Length", oFile.Length.ToString())
''HttpContext.Current.Response.AddHeader("Content-Length", arrByte.Length.ToString())
'HttpContext.Current.Response.ContentType = "application/pdf"
''HttpContext.Current.Response.ContentType = "application/download"
''HttpContext.Current.Response.ContentType = "application/octet-stream"
''HttpContext.Current.Response.BinaryWrite(arrByte)
''HttpContext.Current.Response.OutputStream.Write(arrByte, 0, arrByte.Length)
''HttpContext.Current.Response.TransmitFile(oFile.FullName, 0, oFile.Length)
'HttpContext.Current.Response.TransmitFile(oFile.FullName)
'HttpContext.Current.Response.Flush()
''File.Delete(pdfPath)
'HttpContext.Current.Response.End()
''End If


Private Sub LoadStreamToStream(ByVal inputStream As Stream, ByVal outputStream As Stream)
    Const bufferSize As Integer = (64 * 1024)
    Dim buffer As Object = New Byte((bufferSize) - 1) {}

    While True
        Dim bytesRead As Object = inputStream.Read(buffer, 0, bufferSize)
        If (bytesRead > 0) Then
            outputStream.Write(buffer, 0, bytesRead)
        End If
        If ((bytesRead = 0) _
                    OrElse (bytesRead < bufferSize)) Then
            Exit While
        End If

    End While
End Sub

我已将三个PDF文件上传到Mega,您可以在此链接中找到它们:

https://mega.co.nz/#!ORAhQDxb!ewotEFGNeecGXwm1V1rGyGy0apFGPSiZee_tYztYrCY

密码:pdfs9587

Right.pdf 是使用File.WriteAllBytes创建的。

Wrong1.pdf 是我尝试下载时创建的,我添加了一个文件长度的标头。

Wrong2.pdf 是响应的结果,不会将长度放在标头中。

我想也许长度标题是问题但经过几次尝试后我发现无论用什么方法下载我使用的文件都会重复这两个错误。

然而,比较这三个文件,我认为长度标题的错误文件越接近严格文件。

我希望你能给我一些建议或解决方案。

感谢。

1 个答案:

答案 0 :(得分:0)

最后,我决定不使用响应对象并打开指向我存储使用File.WriteAllBytes方法创建的文件的服务器中的虚拟目录的链接。我无法通过回应使其发挥作用。

相关问题