Open different content type of files.

Posted on 2008-11-18
Last Modified: 2013-12-17
I have the following:

Protected Sub GridViewAttachments_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridViewAttachments.RowCommand
        If e.CommandName = "Browse" Then

            Dim myConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString)

            Dim index As Integer = Convert.ToInt32(lblInterviewID.Text)

                Dim ds As New DataSet

                Dim da As SqlClient.SqlDataAdapter

                Dim arrContent As Byte()

                Dim dr As DataRow

                Dim strSql As String
                strSql = "Select * from [tbl_dotnet_attachments] Where [AttachmentID]='" + e.CommandArgument.ToString() + "'"

                Dim myCommand As New SqlCommand(strSql, myConnection)

                da = New SqlClient.SqlDataAdapter(strSql, myConnection)


                dr = ds.Tables(0).Rows(0)

                arrContent = CType(dr.Item("ImageContent"), Byte())

                Dim filePath As String = dr.Item("Filename").ToString()

                Dim dotPosition As Integer = filePath.LastIndexOf(".")

                Dim filenameOnly As String = filePath.Substring(dotPosition + 1)

                Select Case filenameOnly
                    Case "zip"
                        Response.ContentType = "application/x-zip-compressed"
                    Case "pdf"
                        Response.ContentType = "application/pdf"
                    Case "csv"
                        Response.ContentType = "application/csv"
                    Case "doc"
                        Response.ContentType = "application/doc"
                    Case "docx"
                        Response.ContentType = "application/docx"
                    Case "xls"
                        Response.ContentType = "application/xls"
                    Case "xlsx"
                        Response.ContentType = "application/xlsx"
                    Case "png"
                        Response.ContentType = "application/png"
                    Case "gif"
                        Response.ContentType = "application/gif"
                    Case "jpg"
                        Response.ContentType = "application/jpg"
                    Case "csv"
                        Response.ContentType = "application/csv"
                    Case "txt"
                        Response.ContentType = "application/txt"
                End Select


            Catch ex As Exception

            End Try

        End If

    End Sub

it fails when I try to write the binary content.
Question by:mathieu_cupryk
    LVL 2

    Accepted Solution


    There are a couple of bits of code I see missing in your example above:

    I would try replacing;
    The content type can always be octet-stream from my experience. Consider giving that a go. The users system generally works out what sort of file it is based on the extension.

    Response.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName) //Make sure to give a filename
    Response.ContentType = "application/octet-stream"

    Response.AddHeader("Content-disposition", "attachment; filename=" + sGenName);

    Also, new microsoft formats (docx etc). It throws an exception with the above code, I forget what it is but have a feeling it's a thread exception. You need to catch and ignore this exception as it doesn't actually cause any issues to the file. Having any Response.Write or HTML generation in the exception will append that to the BinaryWrite which will corrupt the document you are sending so be sure not to do that.

    Author Comment

    i was missing Response.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName) //Make sure to
    it is working.

    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK ( for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
    More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    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

    19 Experts available now in Live!

    Get 1:1 Help Now