【问题标题】:how to generate code for download a music (mp3) in asp.net c#?如何在asp.net c#中生成下载音乐(mp3)的代码?
【发布时间】:2017-03-22 22:03:16
【问题描述】:

我有一个 asp.net 网络表单应用程序(音乐网站)。 我想要一个下载按钮。所以当用户点击它时。选择的音乐开始使用浏览器下载。我该怎么做?

这是我尝试过的代码:

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

【问题讨论】:

  • 你有没有尝试过?
  • 是的,我已经为此编写了一个代码,但是以 .file 格式而不是 .mp3 保存的音乐我编写了有问题的代码
  • 您正在做的 test2 子字符串是从文件名中修剪 .mp3 。如果是这样,那是你的问题。传递给内容处置的文件名部分的值是您将在浏览器中看到的值。如果它没有扩展名,则下载为 .file。

标签: c# asp.net webforms


【解决方案1】:

试试这个:

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();

如果你不使用数据库,那么试试这个:

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.");
    }
}

【讨论】:

  • 在数据库中,我有一个音乐表,其列是 ID、音乐名、歌手、日期发布、路径(音乐保存在其中的路径,例如:../music/musicname.mp3)我做没有数据列。
【解决方案2】:

试试这个,你可能会有想法

 protected void Button1_Click(object sender, EventArgs e)
    {

       Response.ContentType = "Application/mp3";
       Response.AppendHeader("Content-Disposition", "attachment;filename=filename.mp3");
       Response.TransmitFile(Server.MapPath("~\Location\filename.mp3"));
       Response.End();
    }

【讨论】:

  • 我尝试了这段代码,但下载后音乐的文件格式不是 .mp3!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-12
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多