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

Hi,

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 = ""
 
            myConnection.Open()
            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
                Response.BinaryWrite(myReader("data"))
            End If
 
            myReader.Close()
            myConnection.Close()
        End Using
    End Sub

Open in new window

kbjornstAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

amxCommented:
myReader("name") should be myReader("filename")
0
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.
0
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.

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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 :)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.