如何在asp.net 3.5中上传mp3文件 [英] How to upload mp3 files in asp.net 3.5

查看:69
本文介绍了如何在asp.net 3.5中上传mp3文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何上传mp3文件以及如何从数据库下载mp3文件...

how to upload mp3 files and how to download mp3 files from database...

推荐答案

上传MP3与上传任何其他文件类似。同样下载。





我建议你先试试,研究,而不是毫不费力地发表你的问题(看起来像家庭作业)一点点,谷歌,拿出一些工作。如果您遇到问题,请发布您遇到的具体问题。



===



BTW,这是询问者所期望的:

1. 首先尝试你想做什么!

2.制定你所做的看似问题/不工作的事情。



尝试一下,告诉你是否面临问题。

各位会员非常乐意为此提供帮助。
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.


你如何在数据库中保存?从你的问题,这是不知道的。所以我可以看到两种方式,你可以下载或保存MP3文件。我将以两种方式完成它们。



1.如果你只保存文件的路径。在这种情况下,您的文件应映射到您网站中的特定文件夹中。然后映射文件名并将文件转换为二进制数组。像这样



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 ); // 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如果将实际文件保存为数据库中的二进制文件,请将该文件作为内存流从数据库中读取。以下代码将帮助您做到这一点


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"))// 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"];
               }
           }
       }





因此请将文件保存到您的网站,您应该拥有上传文件的机制。怎么样?看下面的代码。注意你的网页应该有 FileUpload 控件。





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));
}





然后将 mp3Bytes 与数据库相同或写入这个二进制文件到你最喜欢的网站目录。怎么样?





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);

< br $>


或数据库



Or to Database

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.


这篇关于如何在asp.net 3.5中上传mp3文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆