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

Asp.net 从SQL数据库读取BLOB对象(PDF文件)时出现InvalidCastException,asp.net,vb.net,Asp.net,Vb.net,当我尝试将数据库中的PDF作为BLOB读取时,我遇到了无效强制转换异常的问题。我能够将文件写入数据库,一点问题也没有,但是,当我试图检索它们时,我得到了InvalidCastException 以下是我正在使用的代码: Protected Sub btnPDF_Click(sender As Object, e As EventArgs) Handles btnPDF.Click ' Request.QueryString["docid"].ToString(); Dim doc

当我尝试将数据库中的PDF作为BLOB读取时,我遇到了无效强制转换异常的问题。我能够将文件写入数据库,一点问题也没有,但是,当我试图检索它们时,我得到了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
似乎认为二进制被传递为整数。这与SQLReader有关吗?在这一点上,我真的不确定问题出在哪里

任何帮助都将不胜感激

非常感谢,

理查德·洛根·贝克

*更新*

此后,我通过将线路更改为以下方式解决了我遇到的问题:

Dim blob() As Byte
        blob = reader.Item("document")
但是,在firefox中不会显示PDF文件,即使我的DB只有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

我认为你的问题来自于你在循环中增加I的方式。在For…Next语句末尾将其递增ChunkSize后,下一个i语句将再次将其递增1。尝试将该行更改为:

i = i + ChunkSize - 1
或者,您也可以在For语句的末尾添加一个Step ChunkSize,并删除我所指的行