2016-11-09 14 views
-4

私はasp.net webformアプリケーション(音楽ウェブサイト)を持っています。 ダウンロードボタンがあります。だからユーザーがそれをクリックしたとき。選択した音楽がブラウザでダウンロードされ始めます。どうすればいいですか?場合どのようにasp.net C#で音楽(mp3)をダウンロードするためのコードを生成するには?

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 [email protected]"; 
     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 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

+0

何かしようとしましたか? –

+0

はい私はそのコードを書いていますが、.file3ではない.file3形式で保存された音楽は 私のコードを書く –

+0

あなたがやっているtest2の部分文字列はファイル名から.mp3を切り取っています。もしそうなら、それはあなたの問題です。コンテンツ処理のファイル名部分に渡される値は、ブラウザに表示される値です。拡張子がない場合は.fileとしてダウンロードされます。 – ThatChris

答えて

0

あなたのアイデア

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(); 
    } 
+0

私はこのコードを試してみましたが、ダウンロードした音楽は.mp3形式ではありません。 –

1

これを試してみてくださいを得ることができ、これを試してみてください。ここ

は、私が試したコードですあなたはデータベースを使用していません次にこれを試してみてください:

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

データベースでは、列にはID、Musicname、Singer、DatePublish、path(音楽はその中に保存されているパス:../music/musicname.mp3)というテーブルがあります。データ列はありません。 –

関連する問題