Asp.Net音訊檔案上傳和播放

iDotNetSpace發表於2010-05-10

一、音訊檔案上傳和播放

       在網上找到一個名叫AspNetPager的第三方控制元件,將AspNetPager.dll檔案引用到專案的Bin中。在網頁中可直接呼叫。引用名稱空間  using Telerik.WebControls;

>>上傳

protected void Button1_Click(object sender, EventArgs e)
    {

         foreach (UploadedFile file in RadUploadContext.Current.UploadedFiles)
            {
                if (file.FileName != null && file.FileName != "")
                {
                    string strTime = memberid.ToString() + "-" + DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
                    string namefile = file.FileName;//獲取檔案原來的名字
                    string namelast = namefile.Substring(namefile.LastIndexOf("."));//獲取檔案的字尾名,LastIndexOf()-返回括號內字元的所在位置
                    string FilePath = System.AppDomain.CurrentDomain.BaseDirectory + @"SaveFilePath/MBmusic/";//檔案上傳的路徑
                    System.IO.FileInfo fi = new System.IO.FileInfo(file.FileName);
                    if (fi.Extension.ToLower() == ".mp3" || fi.Extension.ToLower() == ".wma" || fi.Extension.ToLower() == ".rm" || fi.Extension.ToLower() == ".wav")
                    {
                        string name = strTime + namelast;//檔案最後儲存的名字
                        mv = "http://www.cnblogs.com/SaveFilePath/MBmusic/" + name;
                        Filesizes = int.Parse(file.ContentLength.ToString());
                        string path = FilePath + name;
                        if (File.Exists(path) == false)
                        {
                            file.SaveAs(FilePath + name);
                            mf.MF_Path = mv;
                            //mf.MF_Path = mv;
                        }
                        else if (File.Exists(path) == true)
                        {
                            name = strTime + "(" + tt + ")" + namelast;
                            file.SaveAs(FilePath + name);
                            mv = "http://www.cnblogs.com/SaveFilePath/MBmusic/" + name;
                            mf.MF_Path = mv;
                            //mf.MF_Path = mv;
                        }
                        if (RemainByteSize >= Filesizes)
                        {
                            try
                            {

                                //將資料插入資料庫
                                
if (addnum > 0 && udpsize > 0)
                                {
                                    //Response.Write("
                                    //Bind();
                                }
                                else
                                {
                                    Response.Write("
                                    return;
                                }
                                num++;
                            }
                            catch { }
                        }
                        else
                        {
                            Response.Write("
                            return;
                        }
                    }
                    else
                    {
                        Response.Write("
                        return;
                    }
                }
                else
                {
                    Response.Write("
                    return;
                }
            }
   
}

>>播放

 1、在播放頁面新增控制元件Literal1 控制元件

 2、在頁面後臺新增呼叫音訊播放器程式碼

       public static string GetFlashText(string url)
    {
        string str = @"
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
   
           
";
        return str;
    }

3、呼叫GetFlashText()方法

public void BindMusic()
    {

if (mfid != 0)
        {
            DataSet dst = mf.Search(mfid);
            if (dst != null && dst.Tables[0].Rows.Count > 0)
            {
                string url = dst.Tables[0].Rows[0]["MF_Path"].ToString();

            string[] strs = url.Split(new char[] { '/' });
            string mvname = strs[strs.Length - 1];
            string url1 = "SaveFilePath/MBmv/" + mvname;
                Literal1.Text = GetFlashText(url);//Literal1呼叫方法播放音訊
             Label1.Text = dst.Tables[0].Rows[0]["MF_FileName"].ToString();
                //txt_FileName.Text = dst.Tables[0].Rows[0]["MF_FileName"].ToString();
                //txt_introduction.Text = dst.Tables[0].Rows[0]["MF_Introduction"].ToString();
            }
        }
    }

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/12639172/viewspace-662340/,如需轉載,請註明出處,否則將追究法律責任。

相關文章