Open different content type of files. vb.net

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)
            Try

                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)

                da.Fill(ds)

                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

                Response.BinaryWrite(arrContent)
                Response.Flush()
                Response.Close()



            Catch ex As Exception

         
            End Try



           
        End If

    End Sub

it fails when I try to write the binary content.
mathieu_cuprykAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

AshleighGreenCommented:
Hello,

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

I would try replacing;
Response.BinaryWrite(arrContent)
Response.Flush()
Response.Close()
with:
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.BinaryWrite(arrContent)
Response.End()

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

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.
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
mathieu_cuprykAuthor Commented:
i was missing Response.AddHeader("Content-disposition", "attachment;filename=" & strFriendlyName) //Make sure to
it is working.
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
.NET Programming

From novice to tech pro — start learning today.