-2

I'm quite new to MVC and doing a To Do List Web application. I want to allow users to attach a file to the task. I've read that I need to store it in a byte type variable.

My controller :

 public ActionResult AddTask (TaskModel t, HttpPostedFileBase file)
    {
        if (Session["UserID"] != null)
        {
            using (ToDoListEntities3 context = new ToDoListEntities3())
            {
                FilesTable ff = new FilesTable();


                t.fileId = Convert.ToInt32(Session["UserID"]);

                ff.FileId = t.fileId;

                if (file.ContentLength > 0)
                {
                    var fileName = Path.GetFileName(file.FileName);
                    var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
                    file.SaveAs(path);
                    ff.FileName = fileName;

                    var content = new byte[file.ContentLength];
                    file.InputStream.Read(content, 0, file.ContentLength);
                    ff.File = content;


                }

I was able to store the ID and the Filename, but I don't quite understand how the FILES are stored in the db. What does it mean to convert a file to byte?

Tornike
  • 197
  • 1
  • 1
  • 9

2 Answers2

0

Files are usually stored in hard memory.

You only need to store the fileName and/or the full path in the database. Then you can retrieve the file through the filename which is in database but from the hard.

But if you want to store it in database you should first convert it to a Byte array.

YekiDige
  • 11
  • 5
-2

You should store the file in the SERVER, NOT the DB, then get the file directory and save it as a string in your Db.