One page PDF don’t open in IE8

Very weired !!! I can open 2 or more PDF pages in IE8, but one page or less IE8 can’t and gives me this error message:

The file is damaged and could not be repaired.

By the way, Chrome can.

I changed the buffer from Object to Byte and it worked !!!

The old code (part of a handler) :


        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

As you see, ExecuteScalar() attached the output to an Object.
I changed this to 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()

No need to context.Response.OutputStream.Write , it is already impeded in context.Response.BinaryWrite

Took me two days.

Advertisements

About Ahmed

Software craftsman, programmer, developer, system/business analyst, DBA and PM.
This entry was posted in ASP.NET, Error Message, Programming, VB.NET and tagged , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s