• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 498
  • Last Modified:

ASP.NET: Read files from database and launch them from the browser


Im trying to read documents (jpeg, zip, doc, pdf etc) from a SQL 2005 database and display/launch the documents from the asp page.

The attached code works for images (jpeg), but not for zip, pdf or doc. I get the error message:

The XML page cannot be displayed
Cannot view XML input using style sheet. Please correct the error and then click the Refresh button, or try again later.
An invalid character was found in text content. Error processing resource 'http://localhost:3969/details.aspx'.

Any ideas how to make it work for other files than images?
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using myConnection As New SqlConnection(SqlDataSource2.ConnectionString)
            Const SQL As String = "select filename, data from files where id=3094"
            Dim myCommand As New SqlCommand(SQL, myConnection)
            Dim MIMEtype As String = ""
            Dim myReader As SqlDataReader = myCommand.ExecuteReader
            If myReader.Read Then
                If (myReader("name")).ToString.Contains(".zip") Then
                    MIMEtype = "application/zip"
                ElseIf (myReader("name")).ToString.ToUpper.Contains(".DOC") Then
                    MIMEtype = "application/msword"
                ElseIf (myReader("name")).ToString.ToUpper.Contains(".PDF") Then
                    MIMEtype = "application/pdf"
                ElseIf (myReader("name")).ToString.ToUpper.Contains(".JPG") Then
                    MIMEtype = "image/jpeg"
                End If
                Response.ContentType = MIMEtype
            End If
        End Using
    End Sub

Open in new window

  • 2
1 Solution
myReader("name") should be myReader("filename")
Christopher KileCommented:
Each of the types you're having trouble with require plugins or add-ons to the browser, which must be installed before they can be read.  I suspect, though, that amx is correct about the problem, as .JPG is a type the browser would recognize without specifiying the MIME type, and that the others aren't being recognized due to this error.

NOTE:  if the fieldname mistake is the only problem, please give amx all the points.
kbjornstAuthor Commented:
amx: Yes, you are correct, but that was just me changing the code a little before posting. Sorry about that.

I found a solution:

By adding the following lines of code

                Response.AddHeader("Content-Type", MIMEtype)
                Response.AddHeader("Content-Disposition", "inline;filename=" & myReader("name"))

Just before the binarywrite, it worked fine.

Christopher KileCommented:
Nice! I'm adding this to my favorites as this could come in handy.  I look forward to seeing more of your own expertise here :)

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now