Question file downloading from SQL Server

timcyberbanjo

New member
Joined
Oct 20, 2008
Messages
3
Programming Experience
5-10
Hi,
I have a web app that downloads an mp3 file from SQL server. (code extract below). It works fine. It prompts the user for a filename on their workstation to save the file locally. What I have trouble with is getting the getting the dialog box to come up with a meaningful default name. It seems to be using the name of the function that it last called in ASP.net as the default file name to save it as.

Is is possible to give it a meaningful default file name (eg myfile.mp3) ?
Maybe this needs to be done with Javascript at the client end?




code extract::
(code behind the download button)...


VB.NET:
   Dim ConnectionString_fileupload As String
        Dim link As String
        ConnectionString_fileupload = GetGlobalParameter("ConnectionString_fileupload")

        Dim myConnection As New SqlConnection(ConnectionString_fileupload)
        Dim id As Integer
        Dim msg As String
        Dim strFileName As String = "c:\test.mp3"

        id = Session("PROGRAMID")
        Dim SQLText As String
        SQLText = "Select SoundFile1 from programs where program_id = " & id
        Dim myCommand As New SqlCommand(SQLText, myConnection)

        Try
            myConnection.Open()
            Dim myDataReader As SqlDataReader

            myDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)

            Do While (myDataReader.Read())
                Response.AddHeader("Content-Disposition", "attachment;filename=" & strFileName)
                Response.ContentType = "audio/mpeg3"
                Try
                    Response.BinaryWrite(myDataReader.Item("SoundFile1"))
                Catch ex As Exception
                    ' it cannot read the sound file, it does not exist or something is weird about the file

                    Session("MSG") = "Cannot read sound file from database"
                    Session("RETURNLINK") = "ProgList.aspx?id=" & Session("PROGRAMORDER") & "&id2=0&id3=0"
                    Response.Redirect("responsemessage.aspx")

                    Exit Do
                End Try
            Loop

            myConnection.Close()
            ' Response.Write("soundfile successfully retrieved!")
        Catch SQLexc As SqlException
            'Response.Write("Read Failed : " & SQLexc.ToString())


            link = "EditionUpload6.apsx?id=" & Session("ID")

            Response.Redirect(link)

        End Try
 

Latest posts

Back
Top