How you saved in the database? From you question this is not known.So I can see two way you can download or save the mp3 file. I will go through them in both ways.
1. If you only save the path of the file. In this case your file should be mapped in a specific folder in you web site. Then map the file name and covert the file to binary array. Like this
string mp3FileName = "E.T - Kety Perry.mp3";
string mp3Path = Server.MapPath("~/mp3/" + mp3FileName );
folder and "E.T - Kety Perry.mp3" in your root web folder.
byte[] mp3 = System.IO.File.ReadAllBytes(mp3Path);
2. If you save the actual file as binary in the database, read the file as memory stream from the database. The following code some how will help you do this
string mp3FileName = "E.T - Kety Perry.mp3";
using (SqlConnection connection = new SqlConnection("connectionString"))
{
connection.Open();
using (SqlCommand command = new SqlCommand("Select Mp3File from mp3Table Where Mp3FileName='" + mp3FileName + "'", connection))
{
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
byte[] mp3Bytes = (byte[])reader["Mp3File"];
}
}
}
So save the file to your web site you should have a mechanism to upload the file.How? look the following code.Note you should have
FileUpload
control in your web page.
if((mp3Uploader.PostedFile != null) && !string.IsNullOrEmpty(mp3Uploader.PostedFile.FileName) && mp3Uploader.PostedFile.ContentLength > 0) {
byte[] mp3Bytes = new Byte[mp3Uploader.PostedFile.ContentLength];
mp3Uploader.PostedFile.InputStream.Read(mp3bytes, 0, Convert.ToInt32(mp3Uploader.PostedFile.ContentLength));
}
Then same the
mp3Bytes
to the database Or Write this binary to your favorite web site directory. How?
System.IO.File.WriteAllBytes(Server.MapPath("~/mp3/"+ System.IO.Path.GetFileName(mp3Uploader.PostedFile.FileName)), mp3Bytes);
Or to Database
string mp3FileName = "E.T - Kety Perry.mp3";
using (SqlConnection connection = new SqlConnection("connectionString"))
{
connection.Open();
using (SqlCommand command = new SqlCommand("INSERT INTO mp3Table(Mp3File, Mp3FileName,Picture) values(@Mp3File,@Mp3FileName)", connection))
{
command.Parameters.Add("@Mp3File", SqlDbType.VarBinary);
command.Parameters.Add("@Mp3FileName", SqlDbType.NVarChar);
command.Parameters["@Mp3File"].Value = mp3Bytes;
command.Parameters["@Mp3FileName"].Value = mp3FileName;
command.ExecuteNonQuery();
}
}
These are the things that you basically do.
Good luck.