2011-02-28 222 views
0


我已經在asp.net web論壇上發佈了這個問題,但沒有人回覆。無法在IE8中打開.pdf文件

使用IE8我無法打開pdf文件的「ONE」頁面,並給我一個錯誤消息「的文件已損壞,無法修復」從SQL Server 2008多個檢索時,但頁面我可以打開沒有問題。

使用Chrome我可以打開任意數量的.pdf頁面。

使用IE8從DB打開的同一頁,可以直接從使用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 

感謝,

艾哈邁德。

+0

以塊爲單位編寫文件的任何原因? – 2011-02-28 11:35:31

回答

0

問題解決了。

我改變了緩衝區從對象到字節,它的工作!

舊代碼(一個處理程序的一部分):

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()附接在輸出到對象。 我改變了這種以字節:

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

阻礙花了兩天時間。

0

完成PDF後將Response.buffer設置爲true並響應.flush。

+0

謝謝VSU,我試過了,但給了我損壞的文件和一條消息「文件已損壞,無法修復」 – Ahmed 2011-02-28 05:44:25

0

似乎太明顯了,但我必須問......你嘗試過嗎?

Response.WriteFile(imgName) 

Link to documentation萬一你不熟悉這個。 :)

+0

感謝Shadow Wizard的回覆 – Ahmed 2011-03-04 20:28:54