my name Is Omer and I’m a student. In my project I had to upload videos to database and also read them from the database. I’m using OLE object field to store byte[]array. But IDK why I Cant convert it back to file. I don’t know if my problem is in the inserting from the database or in the selecting. my Inserting code:
x
string username = RegisterFunctions.getUsername(Session["userInfo"]);
string heroName = Hero.Text;
VideoService vid = new VideoService();
string path = vid.SaveFileInFoder(myUpload.PostedFile, "potg", Server);
byte[] videoBy = vid.ConvertFileToByte(path);
vid.UploadVideo(heroName,username,videoBy);
SaveFileInFoder:
public string SaveFileInFoder(HttpPostedFile file, string folder, HttpServerUtility Server)
{
string location;
if (file != null && file.ContentLength > 0)
{
location = Server.MapPath(folder) + "\" + System.IO.Path.GetFileName(file.FileName);
try
{
file.SaveAs(location);
}
catch (Exception ex)
{
location = ex.Message;
}
}
else
{
location = null;
}
return location;
}
UploadVideo:
public void UploadVideo(string hero, string username , byte[] file)
{
OleDbConnection conn = new OleDbConnection(Connect.GetConnectionString());
conn.Open();
try
{
string sql = "Insert Into videos(username, hero, file) Values('"+username+"','"+hero+"','"+file+"') ";
OleDbCommand com = new OleDbCommand(sql, conn);
com.ExecuteNonQuery();
com.Dispose();
}
catch (Exception ex)
{
throw ex;
}
finally
{
conn.Close();
}
}
the selecting part:
DataTable table = VideoService.SelectVideos();
videoLength = table.Rows.Count.ToString();
string path = Server.MapPath("/potg");
System.IO.DirectoryInfo di = new DirectoryInfo(path);
for (int i = 0; i < table.Rows.Count; i++)
{
videoList += "var video1 = new video('" + table.Rows[i]["hero"] + "',"
+ table.Rows[i]["videoid"] + ",'" + table.Rows[i]["username"] + "','" +
table.Rows[i]["rating"] + "');" + "videos[" + i + "] = video1;";
File.WriteAllBytes(path + "\" + table.Rows[i]["videoid"] + ".mp4", (byte[])table.Rows[i]["file"]);
}
Advertisement
Answer
For the sake of simplicity I am saving file data as VarBinary(MAX) in the table. For big files ( bigger than ~1MB) you probably will use BLOB or FileStream.
Simple Get File method : (omitting validation and try ,catch)
public byte[] GetVideo(string movieName)
{
byte[] file;
using (var stream = new FileStream(string.Format(@"C:UsersDataVideos{0}.mp4", movieName), FileMode.Open, FileAccess.Read))
{
using (var reader = new BinaryReader(stream))
{
file = reader.ReadBytes((int)stream.Length);
}
}
return file;
}
Save Data in DB (try avoiding using SQL reserved word for column names, like ‘file’)
public void UploadVideo(string hero, string username, byte[] file)
{
// OleDbConnection implements IDisposable
using (OleDbConnection conn = new OleDbConnection(Connect.GetConnectionString()))
{
conn.Open();
// DbCommand implements IDisposable
using (OleDbCommand cmd = conn.CreateCommand())
{
// command with placeholders in order to prevent sql injection
// NOTE : OleDb does not support named parameters, so ? symbol is used here
cmd.CommandText = "Insert Into videos ([username], [hero], [movieFile]) VALUES(?, ?, ?)";
// add parameters
cmd.Parameters.AddWithValue("@username", username);
cmd.Parameters.AddWithValue("@hero", hero);
cmd.Parameters.AddWithValue("@file", file);
// execute
cmd.ExecuteNonQuery();
}
}
}
Get Data from DB and save as MP4 file
public void SaveVideos(string videoName)
{
DataTable table = VideoService.SelectVideos();
string path =string.Format(@"C:UsersDataVideos{0}.mp4",videoName);
for (int i = 0; i < table.Rows.Count; i++)
{
//...
//...
//...
File.WriteAllBytes(path, (byte[])table.Rows[i]["movieFile"]);
}
}