Меня зовут Омер, и я студент. Мой проект - это веб-сайт, в веб-сайте есть пункт, что пользователь будет загружать файл. веб-сайт помещает его в базу данных, а затем показывает его, но почему-то все, что я получаю из базы данных, это "System.Byte []", который получает байты видео. idk знаю, что не так в моем коде. help pls.
Код моей библиотеки "my video helper":
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public class MyVideoHelper
{
public MyVideoHelper()
{
}
//הפונקציה מקבלת מסלול של קובץ ומחזירה את הקובץ מומר לבייטים
public byte[] ConvertFileToByte(string location)
{
byte[] video = null;
FileStream fs = new FileStream(location, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
int length = (int)(fs.Length);
video = br.ReadBytes(length);
return video;
}
//הפונקציה מקבלת קובץ תיקייה ושרת ושומרת את הקובץ בתיקייה
//בשביל השרת פשוט תמיד תכתוב this
public static 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;
}
}
my c sharp code:
using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
public partial class potg : System.Web.UI.Page
{
public string username = "";
public string span = "";
protected void Page_Load(object sender, EventArgs e)
{
string check = (string)(Session["loggedin"]);
if (check == "true")
{
string id2 = (string)(Session["DataTable"]);
int number = int.Parse(id2);
string command2 = "SELECT username FROM users";
DataTable table = MyAdoHelper.ExecuteDataTable("omerwatchdb.accdb", command2);
int length = table.Rows.Count;
string command3 = "SELECT profilePic FROM users";
DataTable table2 = MyAdoHelper.ExecuteDataTable("omerwatchdb.accdb", command3);
username = "<div id='sidebar'> username:" + table.Rows[number - 1]["username"] + "<img id='profilePicRight'src='profile/" + table2.Rows[number - 1]["profilePic"] + ".png'></div>";
MyVideoHelper x = new MyVideoHelper();
string path = "";
if (Session["loggedIn"] != "true")
{
Response.Write("<script>alert('You Are not SignIN!');</script>");
return;
}
if ((video2.PostedFile != null) && (video2.PostedFile.ContentLength > 0))
{
string hero3 = Request.Form["hero3"];
path = MyVideoHelper.SaveFileInFoder(video2.PostedFile, "potg", Server);
string command = "INSERT INTO videos (hero, file) VALUES( '" + hero3 + "','" + x.ConvertFileToByte(path) + "')";
MyAdoHelper.DoQuery("omerwatchdb.accdb", command);
}
}
string fileName = "omerwatchdb.accdb";
string sql = "SELECT file,hero FROM VIDEOS";
DataTable dt = MyAdoHelper.ExecuteDataTable(fileName, sql);
int i = dt.Rows.Count;
for (int c = 0; c < i; c++)
{
byte[] vid = (byte[])dt.Rows[c][0];
string hero = (string)dt.Rows[c][1];
string path3 = Server.MapPath("/potg") + "\\" + hero +c+ ".mp4";
string name = Path.GetFileName(path3);
File.WriteAllBytes(path3, vid);
span += "<li data-name='" + name + "' onclick='potgShow(this)'><a href='#'>KobiMarimi " + hero + " </a></li>";
}
}
}
my код библиотеки "MyAdoHelper":
using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OleDb;
/// <summary>
/// Summary description for MyAdoHelper
/// פעולות עזר לשימוש במסד נתונים מסוג אקסס
/// App_Data המסד ממוקם בתקיה
/// </summary>
public class MyAdoHelper
{
public MyAdoHelper()
{
//
// TODO: Add constructor logic here
//
}
public static OleDbConnection ConnectToDb(string fileName)
{
string path = HttpContext.Current.Server.MapPath("App_Data/");//מיקום מסד בפורוייקט
path += fileName;
//string path = HttpContext.Current.Server.MapPath("App_Data/" + fileName);//מאתר את מיקום מסד הנתונים מהשורש ועד התקייה בה ממוקם המסד
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;Data source=" + path;//נתוני ההתחברות הכוללים מיקום וסוג המסד
OleDbConnection conn = new OleDbConnection(connString);
return conn;
}
/// <summary>
/// To Execute update / insert / delete queries
/// הפעולה מקבלת שם קובץ ומשפט לביצוע ומבצעת את הפעולה על המסד
/// </summary>
public static void DoQuery(string fileName, string sql)//הפעולה מקבלת שם מסד נתונים ומחרוזת מחיקה/ הוספה/ עדכון
//ומבצעת את הפקודה על המסד הפיזי
{
OleDbConnection conn = ConnectToDb(fileName);
conn.Open();
OleDbCommand com = new OleDbCommand(sql, conn);
com.ExecuteNonQuery();
com.Dispose();
conn.Close();
}
/// <summary>
/// To Execute update / insert / delete queries
/// הפעולה מקבלת שם קובץ ומשפט לביצוע ומחזירה את מספר השורות שהושפעו מביצוע הפעולה
/// </summary>
public int RowsAffected(string fileName, string sql)//הפעולה מקבלת מסלול מסד נתונים ופקודת עדכון
//ומבצעת את הפקודה על המסד הפיזי
{
OleDbConnection conn = ConnectToDb(fileName);
conn.Open();
OleDbCommand com = new OleDbCommand(sql, conn);
int rowsA = com.ExecuteNonQuery();
conn.Close();
return rowsA;
}
/// <summary>
/// הפעולה מקבלת שם קובץ ומשפט לחיפוש ערך - מחזירה אמת אם הערך נמצא ושקר אחרת
/// </summary>
public static bool IsExist(string fileName, string sql)//הפעולה מקבלת שם קובץ ומשפט בחירת נתון ומחזירה אמת אם הנתונים קיימים ושקר אחרת
{
OleDbConnection conn = ConnectToDb(fileName);
conn.Open();
OleDbCommand com = new OleDbCommand(sql, conn);
OleDbDataReader data = com.ExecuteReader();
bool found;
found = (bool)data.Read();// אם יש נתונים לקריאה יושם אמת אחרת שקר - הערך קיים במסד הנתונים
conn.Close();
return found;
}
//רועי
public static DataTable ExecuteDataTable(string fileName, string sql)
{
OleDbConnection conn = ConnectToDb(fileName);
conn.Open();
OleDbDataAdapter tableAdapter = new OleDbDataAdapter(sql, conn);
DataTable dt = new DataTable();
tableAdapter.Fill(dt);
return dt;
}
public void ExecuteNonQuery(string fileName, string sql)
{
OleDbConnection conn = ConnectToDb(fileName);
conn.Open();
OleDbCommand command = new OleDbCommand(sql, conn);
command.ExecuteNonQuery();
conn.Close();
}
public static string printDataTable(string fileName, string sql)//הפעולה מקבלת שם קובץ ומשפט בחירת נתון ומחזירה אמת אם הנתונים קיימים ושקר אחרת
{
DataTable dt = ExecuteDataTable(fileName, sql);
string printStr = "<table border='1'>";
foreach (DataRow row in dt.Rows)
{
printStr += "<tr>";
foreach (object myItemArray in row.ItemArray)
{
printStr += "<td>" + myItemArray.ToString() + "</td>";
}
printStr += "</tr>";
}
printStr += "</table>";
return printStr;
}
}