简体   繁体   中英

ASP.NET and C# upload a mp3 file to a database

I want to upload a music file to a database, but i don't know how. I need to upload the file to server and then upload it to the database? Or I can do everything at the same time? And how i can do it?

There is no shortage of tutorials on how to do this. Here's a decent one . Basically, what you're asking is how to store a file as a binary stream in a database field.

However, @Saif al Harthi makes a good point in his comment. It's generally considered bad practice to store a binary file in a relational database. Are you sure this is what you want to do? Your server already has a fairly efficient means of storing/retrieving files... the file system. Unless there's a compelling reason to store the file in the database, it's usually better practice to store it on the file system and just write a database record that references the file (path, maybe type, other application-specific data about it, etc.). The file's name can be changed to, say, the primary identifier for the database table in order to easily reference between them.

It's a little more work, but it's a little better for the server and makes use of the right tools for the right jobs. That is, of course, unless you have a compelling reason for keeping a binary file in a relational database. If there's a reason, please share it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM