public void SaveFile(TeacherUploadViewModel model, HttpServerUtilityBase server, int teacherId, KnowledgeChannelEntities db)
 {
     var file = model.Resource.ResourceFile;
     if (file.ContentLength > 0)
     {
         var teacher = db.Teachers.Where(x => x.TeacherID == teacherId).SingleOrDefault();
         var fileName = Path.GetFileName(file.FileName);
         var folderName = Path.Combine(server.MapPath("~/FileResources"), teacher.TeacherID.ToString());
         var folderNameTosave = Path.Combine("FileResources", teacher.TeacherID.ToString());
         if(!Directory.Exists(folderName))
         {
             Directory.CreateDirectory(folderName);
         }
         var pathFileServer = Path.Combine(folderName, fileName);
         file.SaveAs(pathFileServer);
         var pathDatabase = Path.Combine(folderNameTosave, fileName);
         SaveInDatabase(model, pathDatabase, teacherId, db);
     }
 }
        public ActionResult UploadFile(TeacherUploadViewModel model)
        {
            if (!Application.IsAuthenticated && Application.TeacherID == 0) {
                return RedirectToAction("SignIn");
            }

            if(model.Resource.Name == ""){
                ViewBag.Header = "Input Invalid";
                ViewBag.Message = "Please fill up Resource Name";
            }

            if (model.Resource.ResourceFile.ContentLength > 26214400) {
                ViewBag.Header = "Exceeded Max File Size";
                ViewBag.Message = "The maximum file size capacity for upload is 25MB";
                return View("Message", "_LayoutGuest");
            }

            var Model = new TeacherUploadViewModel(model, Application.Db);
            Model.SaveFile(model, Server, Application.TeacherID, Application.Db);
            return View(Model);
        }
 //POST
 public TeacherUploadViewModel(TeacherUploadViewModel model, KnowledgeChannelEntities db)
 {
     this.model = model;
     Message = "Thank you. Your file has been uploaded.";
     PopulateLevelsAndSubjects(db);
 }
 private void SaveInDatabase(TeacherUploadViewModel Model,string path, int TeacherID, KnowledgeChannelEntities db)
 {
     var resource = db.Resources.CreateObject();
     resource.DateCreated = DateTime.Now;
     resource.Name = Model.Resource.Name;
     resource.Description = Model.Resource.Description;
     resource.ResourceFile = path;
     resource.LevelID = Model.Resource.ResourceLevel;
     resource.SubjectID = Model.Resource.ResourceSubject;
     resource.TeacherID = TeacherID;
     db.Resources.AddObject(resource);
     db.SaveChanges();
 }