Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
765 views
in Technique[技术] by (71.8m points)

how to generate code for download a music (mp3) in asp.net c#?

I have a asp.net webform application (music website). I want to have a download button. So when users clicked on that. selected music start to download with browser.How can I do that?

here is the code that I tried:

string id_new;
id_new = Session["selectedmusicID"].ToString();
DataTable dt2 = new DataTable();
dt2 = blm.selectMusic("sel_music", Convert.ToInt32(id_new));
string test = dt2.Rows[0][9].ToString();
string test2 = test.Substring(9);
Context.Response.Clear();
Context.Response.Buffer = true;
Context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + test2);
Context.Response.ContentType = "audio/mp3";
Context.Response.TransmitFile(@"~music" + test2 );
Context.Response.End();

see this picture

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this:

int id = int.Parse(context.Request.QueryString["id"]);
byte[] bytes;
string contentType;
string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string name;

using (SqlConnection con = new SqlConnection(strConnString))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "select Name, Data, ContentType from tblFiles where Id=@Id";
        cmd.Parameters.AddWithValue("@Id", id);
        cmd.Connection = con;
        con.Open();
        SqlDataReader sdr = cmd.ExecuteReader();
        sdr.Read();
        bytes = (byte[])sdr["Data"];
        contentType = sdr["ContentType"].ToString();
        name = sdr["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();

If you're not using database then try this:

string FileName = dateTimeStamp + "SiteReservation.doc";

if (FileName != "")
{
    System.IO.FileInfo file = new System.IO.FileInfo(path + dateTimeStamp + "SiteReservation.doc");

    if (file.Exists)
    {
        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment; filename=SiteReservation.doc");
        Response.AddHeader("Content-Length", file.Length.ToString());
        Response.ContentType = "application/octet-stream";
        Response.WriteFile(file.FullName);
        //Response.End();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    else
    {
        Response.Write("This file does not exist.");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...