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

Posted on 2008-11-13
Last Modified: 2012-05-05

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

Question by:kbjornst
    LVL 9

    Expert Comment

    myReader("name") should be myReader("filename")
    LVL 23

    Expert Comment

    by:Christopher Kile
    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.

    Accepted Solution

    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.

    LVL 23

    Expert Comment

    by:Christopher Kile
    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

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    Suggested Solutions

    This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
    For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
    Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
    Viewers will learn how the fundamental information of how to create a table.

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now