Click to See Complete Forum and Search --> : Trouble Displaying Word Doc or PDF in .Net


jazzyj99
04-17-2007, 05:46 PM
Can Anyone tell me how to modify my code to let me view word documents and pdfs from a .aspx page???

I'm only able to view jpg, gifs, and bmps from the following code.


ShowPicture.aspx.vb
Imports System.Data.SqlClient

Partial Class ShowPicture
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim PictureID As Integer = Convert.ToInt32(Request.QueryString("PictureID"))

'Connect to the database and bring back the image contents & MIME type for the specified picture
Using myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ImageGalleryConnectionString").ConnectionString)

Const SQL As String = "SELECT [MIMEType], [ImageData] FROM [Pictures] WHERE [PictureID] = @PictureID"
Dim myCommand As New SqlCommand(SQL, myConnection)
myCommand.Parameters.AddWithValue("@PictureID", PictureID)

myConnection.Open()

Dim myReader As SqlDataReader = myCommand.ExecuteReader
If myReader.Read Then
Response.ContentType = myReader("MIMEType").ToString()
Response.BinaryWrite(myReader("ImageData"))
End If
myReader.Close()
myConnection.Close()
End Using
End Sub
End Class

PeOfEo
04-17-2007, 06:02 PM
If I am understanding you correctly, the answer is you can't. The browser cannot display word documents and pdfs (without the use of a plugin). All you are going to be able to do is seed them to the user so that he can open them. However you can read these documents on the server and write out their contents in a form the user's browser can handle. Which is it that you are after? Simply letting the user download these documents or do you want to parse the documents and display the contents? In either case, the code is going to be completely different then what you have posted above.