how to read and write MP3 to database

后端 未结 2 1762
一个人的身影
一个人的身影 2021-01-07 14:24

how to read MP3 from Sql database. in sql i have stored the file as binary format. now i want to retrive the Mp3 file stored in the sql and show in my aspx page. how????

相关标签:
2条回答
  • 2021-01-07 14:52

    You'd probably want to use a Generic ASHX Handler that retrieves the binary data and streams it to the response stream with the correct content-type header ("audio/mpeg").

    If you look at the article Displaying Images in ASP.NET Using HttpHandlers then you should see the basic principle. You just need to change the content-type output.

    0 讨论(0)
  • 2021-01-07 14:58

    In its simplest form this is how you would get the raw bytes, can't really show any more without knowing what you want it for...

    private byte[] GetMp3Bytes(string connString)
    {
       SqlConnection conn = null;
       SqlCommand cmd = null;
       SqlDataReader reader = null;
    
       using (conn = new SqlConnection(connString))
       {
          conn.Open();
    
          using (cmd = new SqlCommand("SELECT TOP 1 Mp3_File FROM MP3_Table", conn))
          using (reader = cmd.ExecuteReader())
          {
              reader.Read();
              return reader["Mp3_File"] as byte[];
          }
       }
    }
    
    0 讨论(0)
提交回复
热议问题