[RESOLVED] Not able to open .pdf file in IE8

Hi Gurus,

 

I can't open "ONE" page of .pdf file using IE8 and give me an error message "the file is damaged and could not be repaired" when retrieved from SQL Server 2008, but more than one page I can open with no problem.

With Chrome I can open any number of .pdf pages.

The same page that I can't open using IE8 from DB, can be opened directly from hard disk using IE8.

My code in .asxh file:

 

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

 

Thanks,

Ahmed.

 

I think when I asked for help yesterday was bad timing , and most of gurus were asleep.

So I'm re-sending the question now, hoping that gurus are awake and have time :-)

Thanks,

Ahmed.

 

Hi,

Ahmed A M

can't open "ONE" page of .pdf file using IE8 and give me an error message "the file is damaged and could not be repaired" when retrieved from SQL Server 2008, but more than one page I can open with no problem.

According to the error message ,I would like to suggest you to check the link below for the similar thread which provides some solutions for the same issue.

http://stackoverflow.com/questions/2440518/the-file-is-damaged-and-could-not-be-repaired

Here is an article which describes about 4 ways to send a PDF file to the IE Client in ASP.NET .

Ahmed A M

With Chrome I can open any number of .pdf pages.

The same page that I can't open using IE8 from DB, can be opened directly from hard disk using IE8.

As far as I know some Web sites may not be displayed correctly or may not work correctly in Windows Internet Explorer 8.

However, this problem does not occur in earlier versions of Internet Explorer.

I would like to suggest you to check the article below for the solutions:

Advanced solutions: Some Web sites may not be displayed correctly or work correctly in Windows Internet Explorer 8

Hope it can help you.

Thanks Ming for reply.

 

I found the solution as follows:

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:

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

Took me two days.

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()

thoughts on "[RESOLVED] Not able to open .pdf file in IE8"

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>