-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?

1
  • 1
    in your code you are storing in ("~/App_Data/uploads) not Db. Commented Jan 16, 2017 at 8:14

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

-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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.