示例#1
0
 protected void SaveLecture(object sender, EventArgs e)
 {
     string savePath = Server.MapPath("~/resources/" + User.Identity.GetUserId() + "/");
     if (!Directory.Exists(savePath))
     {
         Directory.CreateDirectory(savePath);
     }
     if (MaterialInput.HasFiles)
     {
         if (ModelState.IsValid)
         {
             using (ResourceContext db = new ResourceContext())
             {
                 Lecture lecture = new Lecture
                 {
                     Title = LectureTitle.Text,
                     Notes = NotesTextBox.Text,
                     DateCreated = DateTime.Now,
                     AuthorId = User.Identity.GetUserId()
                 };
                 foreach (HttpPostedFile uploadedFile in MaterialInput.PostedFiles)
                 {
                     uploadedFile.SaveAs(Path.Combine(savePath, uploadedFile.FileName));
                     Material material = new Material
                     {
                         Filename = uploadedFile.FileName,
                         ContentType = uploadedFile.ContentType,
                         ContentLength = uploadedFile.ContentLength,
                         DateUploaded = DateTime.Now
                     };
                    lecture.Materials.Add(material);
                 }
                 db.LecturesContext.Add(lecture);
                 db.SaveChanges();
             }
         }
     }
     Response.Redirect("Lectures");
 }