Click here to Skip to main content
15,893,337 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to upload mp3 files and how to download mp3 files from database...
Posted
Updated 30-Oct-17 19:53pm
Comments
Sandeep Mewara 7-May-11 14:24pm    
Tried anything?

Uploading MP3 would be similar to uploading any other file. Same for download.


Instead of posting your questions without any effort (that looks like homework statement), I would suggest you to try them first, research a little, Google, come up with some work. If you get stuck then post specific issues that you face.

===

BTW, here is what is expected by enquirers:
1. TRY first what you want to do!
2. Formulate what was done by you that looks like an issue/not working.

Try them and tell if you face issues.
Members will be more than happy to help like this.
 
Share this answer
 
Comments
Ed Nutting 7-May-11 17:46pm    
My 5 - it's the same old advice as always... when will people learn?.... ;P
Wonde Tadesse 7-May-11 20:14pm    
Agreed.
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

C#
string mp3FileName = "E.T - Kety Perry.mp3";
string mp3Path = Server.MapPath("~/mp3/" + mp3FileName ); // Note: I am assuming that you have "mp3" 
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
C#
string mp3FileName = "E.T - Kety Perry.mp3";
       using (SqlConnection connection = new SqlConnection("connectionString"))// Replace based on your Connection srting
       {
           connection.Open();
           // Assuming that you do have a Table called Mp3Table with Mp3File(varbinary(MAX)) data type and Mp3FileName(nvarchar(50)) data type
           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.

C#
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?

C++
System.IO.File.WriteAllBytes(Server.MapPath("~/mp3/"+ System.IO.Path.GetFileName(mp3Uploader.PostedFile.FileName)), mp3Bytes);


Or to Database
C#
string mp3FileName = "E.T - Kety Perry.mp3";
        using (SqlConnection connection = new SqlConnection("connectionString"))// Replace based on your Connection srting
        {
            connection.Open();
            // Assuming that you do have a Table called Mp3Table with Mp3File(varbinary(MAX)) data type and Mp3FileName(nvarchar(50)) data type
            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.
 
Share this answer
 
v2
Comments
Ed Nutting 7-May-11 17:47pm    
Very good answer, though you should probably read what Sandeep said, you shouldn't do all the work for people ;P Never the less, a superb answer :)
Wonde Tadesse 6-Dec-11 10:37am    
Agreed.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900