从SQL数据库读取BLOB对象(PDF文件)时出现InvalidCastException

时间:2011-11-03 21:54:38

标签: asp.net vb.net

当我尝试将数据库中的PDF作为BLOB读取时,我遇到了无效的Cast Exception问题。我能够将文件写入数据库没有任何问题,但是,当我尝试检索它们时,我只是得到InvalidCastException。

以下是我正在使用的代码:

Protected Sub btnPDF_Click(sender As Object, e As EventArgs) Handles btnPDF.Click
    ' Request.QueryString["docid"].ToString();
    Dim docuid As String = "b39a443d-ccfd-47f4-b333-f12cd94683d6"
    'Connection and Parameters
    Dim param As SqlParameter = Nothing
    Dim conn As SqlConnection = New SqlConnection(
       ConfigurationManager.ConnectionStrings("menu").ToString())
    Dim cmd As New SqlCommand("sp_getdoc", conn)
    cmd.CommandType = CommandType.StoredProcedure
    param = New SqlParameter("@docuid", SqlDbType.NVarChar, 100)
    param.Direction = ParameterDirection.Input
    param.Value = docuid
    cmd.Parameters.Add(param)
    'Open connection and fetch the data with reader
    conn.Open()
    Dim reader As SqlDataReader =
      cmd.ExecuteReader(CommandBehavior.CloseConnection)
    If reader.HasRows Then
        reader.Read()
        '
        Dim doctype As String = reader("doctype").ToString()
        Dim docname As String = reader("docname").ToString()
        '
        Response.Buffer = False
        Response.ClearHeaders()
        Response.ContentType = doctype
        Response.AddHeader("Content-Disposition",
                 "attachment; filename=" + docname)
        '
        'Code for streaming the object while writing
        Const ChunkSize As Integer = 1024
        Dim buffer() As Byte = New Byte(ChunkSize) {}
        Dim binary(reader("document")) As Byte
        Dim ms As New MemoryStream(binary)
        Dim SizeToWrite As Integer = ChunkSize
        For i As Integer = 0 To binary.GetUpperBound(0) - 1 Step i = i + ChunkSize
            If Not Response.IsClientConnected Then
                Return
            End If
            If i + ChunkSize >= binary.Length Then
                SizeToWrite = binary.Length - i
            End If
            Dim chunk(SizeToWrite) As Byte
            ms.Read(chunk, 0, SizeToWrite)
            Response.BinaryWrite(chunk)
            Response.Flush()
        Next
        Response.Close()
    End If
End Sub

我在以下问题专门遇到问题:

Dim binary(reader("document")) As Byte

似乎认为二进制文件正在传递一个Integer。这与SQLReader有关吗?我现在还不确定问题是什么。

非常感谢任何帮助。

非常感谢,

Richard E Logan-Baker

*的 更新 *

我已经通过将行更改为:

来解决我遇到的问题
Dim blob() As Byte
        blob = reader.Item("document")

然而,PDF并没有在firefox中显示,当我保存文件时(即使我的数据库只有2MB~),下载超过40MB的数据也非常高兴!此外,文件大小报告为未知。我现在真的被困住了。

*的 更新 *

我现在已经在浏览器中打开了PDF,但是没有显示数据,Adobe Acrobat表示从文件中提取文本/字体时出现问题,并且PDF以某种方式被破坏。

这是我现在更新的代码:

Protected Sub btnPDF_Click(sender As Object, e As EventArgs) Handles btnPDF.Click
    ' Request.QueryString["docid"].ToString(); 
    Dim docuid As String = "ba32bf45-1b5c-451a-969c-290dc2cf9073"
    'Connection and Parameters
    Dim param As SqlParameter = Nothing
    Dim conn As SqlConnection = New SqlConnection(
       ConfigurationManager.ConnectionStrings("menu").ToString())
    Dim cmd As New SqlCommand("sp_getdoc", conn)
    cmd.CommandType = CommandType.StoredProcedure
    param = New SqlParameter("@docuid", SqlDbType.NVarChar, 100)
    param.Direction = ParameterDirection.Input
    param.Value = docuid
    cmd.Parameters.Add(param)
    'Open connection and fetch the data with reader
    conn.Open()
    Dim reader As SqlDataReader =
      cmd.ExecuteReader(CommandBehavior.CloseConnection)
    If reader.HasRows Then
        reader.Read()
        '
        Dim doctype As String = reader("doctype").ToString()
        Dim docname As String = reader("docname").ToString()
        '
        Response.Buffer = False
        Response.ClearHeaders()
        Response.ContentType = doctype
        Response.AddHeader("Content-Disposition",
                "attachment; filename=" + docname)
        '
        'Code for streaming the object while writing
        Const ChunkSize As Integer = 1024
        Dim buffer() As Byte = New Byte(ChunkSize) {}
        Dim blob() As Byte
        blob = reader.Item("document")
        Dim ms As New MemoryStream(blob)
        Dim SizeToWrite As Integer = ChunkSize
        For i As Integer = 0 To blob.GetUpperBound(0) - 1
            If Not Response.IsClientConnected Then
                Return
            End If
            If i + ChunkSize >= blob.Length Then
                SizeToWrite = blob.Length - i
            End If
            Dim chunk(SizeToWrite) As Byte
            ms.Read(chunk, 0, SizeToWrite)
            Response.BinaryWrite(chunk)
            Response.Flush()
            i = i + ChunkSize
        Next i
        Response.Close()
    End If
End Sub

1 个答案:

答案 0 :(得分:0)

我认为你的问题来自你在循环内增加“i”的方式。在For ... Next语句结束时通过ChunkSize递增它之后,“Next i”语句将再次将其递增1。尝试将该行更改为:

i = i + ChunkSize - 1

或者您可以在For语句的末尾添加“Step ChunkSize”并删除我所指的行。