无法在IE8中打开.pdf文件

时间:2011-02-28 04:30:39

标签: asp.net vb.net pdf


我在asp.net网络论坛上发布了这个问题,但没有人回复。

我无法使用IE8打开.pdf文件的“ONE”页面,并从SQL Server 2008中检索时出现错误消息“文件已损坏且无法修复”,但我可以打开不止一个页面。

使用Chrome,我可以打开任意数量的.pdf页面。

我无法使用IE8中的IE8打开的同一页面,可以使用IE8直接从硬盘打开。

我在.asxh文件中的代码:

    context.Response.ContentType = "application/pdf" 
    Dim strm As Stream = ShowNewsImage(imgName) 
    If Not strm Is Nothing Then 
        Dim buffer As Byte() = New Byte(4095) {} 
        Dim byteSeq As Integer = strm.Read(Buffer, 0, 4096) 

        Do While byteSeq > 0 
            context.Response.OutputStream.Write(buffer, 0, byteSeq) 
            byteSeq = strm.Read(Buffer, 0, 4096) 
        Loop 
        context.Response.BinaryWrite(buffer) 
    End If 

谢谢,

艾哈迈德。

3 个答案:

答案 0 :(得分:0)

完成PDF后,将Response.buffer设置为true,并将response.flush设置为。

答案 1 :(得分:0)

似乎太明显但我不得不问......你试试这个吗?

Response.WriteFile(imgName)

Link to documentation如果你不熟悉这个。 :)

答案 2 :(得分:0)

问题解决了。

我将缓冲区从Object更改为Byte,它工作正常!!!

旧代码(处理程序的一部分):

context.Response.ContentType = "application/pdf"
    Dim strm As Stream = ShowNewsImage(imgName)
    If Not strm Is Nothing Then
        Dim buffer As Byte() = New Byte(4095) {}
        Dim byteSeq As Integer = strm.Read(buffer, 0, 4096)

        Do While byteSeq > 0
            context.Response.OutputStream.Write(buffer, 0, byteSeq)
            byteSeq = strm.Read(buffer, 0, 4096)
        Loop
        context.Response.BinaryWrite(buffer)
        context.Response.End()
    End If
End Sub

Public Function ShowNewsImage(ByVal imgName As String) As Stream
    Dim conn As String = ConfigurationManager.ConnectionStrings("Connection").ConnectionString
    Dim connection As SqlConnection = New SqlConnection(conn)
    Dim sql As String = "SELECT image FROM Table WHERE ID = @ID"
    Dim cmd As SqlCommand = New SqlCommand(sql, connection)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.AddWithValue("@ID", imgName)
    connection.Open()
    Dim img As <strong>Object </strong>= cmd.ExecuteScalar()
    Try
        Return New MemoryStream(CType(img, Byte()))
    Catch
        Return Nothing
    Finally
        connection.Close()
    End Try
End Function

如您所见,ExecuteScalar()将输出附加到Object。 我把它改成了Byte:

context.Response.ContentType = "application/pdf"
    Dim buffer As Byte() = New Byte(4095) {}
    Dim byteSeq As Integer = 0
    Dim conn As String = ConfigurationManager.ConnectionStrings("Connection").ConnectionString
    Dim connection As SqlConnection = New SqlConnection(conn)
    Dim sql As String = "SELECT image FROM Table WHERE ID = @ID"
    Dim cmd As SqlCommand = New SqlCommand(sql, connection)
    cmd.CommandType = CommandType.Text
    cmd.Parameters.AddWithValue("@ID", imgName)
    connection.Open()

    buffer = cmd.ExecuteScalar()

    context.Response.BinaryWrite(buffer)
    context.Response.End()

不需要context.Response.OutputStream.Write,它已经在context.Response.BinaryWrite中被阻止了

我花了两天时间。