Click here to Skip to main content
15,913,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to upload mp3 song and download it also..
i have correct code but problem is that when i click on upload button then it does'nt go to button click event so that code is not work so what can i do ???plz help me..

my code is below:-
C#
protected void btnUpload_Click(object sender, EventArgs e)
   {
       using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
       {
           byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
           //string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
           //using (SqlConnection con = new SqlConnection(strConnString))
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "INSERT INTO DSActor.DTL_SONGS(SONG_NAME,SONG_EXTENSION,SONG_BINARY)" + " VALUES (@SONG_NAME,@SONG_EXTENSION,@SONG_BINARY)";
                   cmd.Parameters.AddWithValue("@SONG_NAME", Path.GetFileName(FileUpload1.PostedFile.FileName));
                   cmd.Parameters.AddWithValue("@SONG_EXTENSION", "audio/mpeg3");
                   cmd.Parameters.AddWithValue("@SONG_BINARY", bytes);
                   cmd.Connection = con;
                   con.Open();
                   cmd.ExecuteNonQuery();
                   con.Close();
               }
           }
       }


       Response.Redirect(Request.Url.AbsoluteUri);

   }



handler code is:-

C#
using System;
using System.Web;
using System.Data.SqlClient;
using System.Configuration;
public class File : IHttpHandler
{

    public void ProcessRequest (HttpContext context)
    {
        int TYPE_ID = int.Parse(context.Request.QueryString["TYPE_ID"]);
        byte[] bytes;
        string contentType;
        //string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        String str = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
        string name;
        using (SqlConnection con = new SqlConnection(str))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select SONG_NAME, SONG_EXTENSION, SONG_BINARY from DSActor.DTL_SONGS where TYPE_ID=@Id";
                cmd.Parameters.AddWithValue("@Id", TYPE_ID);
                cmd.Connection = con;
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();
                sdr.Read();
                bytes = (byte[])sdr["SONG_BINARY"];
                contentType = sdr["SONG_EXTENSION"].ToString();
                name = sdr["SONG_NAME"].ToString();
                con.Close();
            }
        }
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + name);
        context.Response.ContentType = contentType;
        context.Response.BinaryWrite(bytes);
        context.Response.End();
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}



and designing code is (grid view and upload button designing code ):=

XML
<div id="tab3" class="tab_content">
            <h2>Mp3 Songs</h2>
            <div >
             <asp:FileUpload ID="FileUpload1" runat="server" />
             <asp:Button ID="btnUpload" runat="server" Text="Upload" OnClick="btnUpload_Click" />
            <asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false" RowStyle-BackColor="#A1DCF2" Font-Names = "Arial" Font-Size = "10pt"
    HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White">
        <Columns>
            <asp:BoundField DataField="Name" HeaderText="FileName" />
            <asp:TemplateField>
                <ItemTemplate>
                    <object type="application/x-shockwave-flash" data='dewplayer-vol.swf?mp3=File.ashx?Id=<%# Eval("TYPE_ID") %>'
                        width="240" height="20" id="dewplayer">
                        <param name="wmode" value="transparent" />
                        <param name="movie" value='dewplayer-vol.swf?mp3=File.ashx?Id=<%# Eval("TYPE_ID") %>'/>
                    </object>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:HyperLinkField DataNavigateUrlFields="Id" Text = "Download" DataNavigateUrlFormatString = "~/File.ashx?Id={0}" HeaderText="Download" />
        </Columns>
    </asp:GridView>





          </div>
        </div>





plz help me..
thanks in advance....
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900