I am building a site where I have to use file upload control for attaching support logs / mails etc... I felt that saving files in a database would be the better option.
I am using the code below to upload files. However, I am not able to test it as I don't know how to retrieve files from a database. Can some one please help me on this?
File type can be anything.
Code:
FileUrl = "C:\\Attachments\\"+Path.GetFileName(UploadCtrl.NavigateUrl);
FileStream fs = new FileStream(FileUrl, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
long numBytes = new FileInfo(FileUrl).Length;
buff = br.ReadBytes(Convert.ToInt32(numBytes));
SqlConnection conn = new SqlConnection(SQLSrc.ConnectionString);
conn.Open();
SqlCommand command = conn.CreateCommand();
string InsertQueryText = "insert into Attachments values ('" + Path.GetFileName(FileUrl) + "','" + MIME(Path.GetExtension(Att_Overview_Link.NavigateUrl)) + "','" + buff + "');";
command.CommandText = InsertQueryText;
command.ExecuteNonQuery();
Here, MIME is the user defined function to get the MIME value of the specified file type.
Frontend: C# ASP.NET and SQL Server as backend
If you are using SQL Server 2008 or more recent, you can use FILESTREAM storage for a varbinary(max) datatype. This MSDN article contains some C# example code that should accomplish what you're trying to do. It also shows how to create the table you will use to store your files.
Start by fixing your code to remove the SQL Injection vulnerability:
NB: I'm not sure what your
UploadCtrl
is, but most file upload controls provide direct access to the uploaded file as aStream
, not a file name on the server. Depending on how this specific control works, you might need to change how you read the uploaded file.To retrieve the file, you would select the relevant name, MIME type and bytes, and write them to the response: