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

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

0
kbjornst
Asked:
kbjornst
  • 2
1 Solution
 
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
 
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

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