public ActionResult UploadGraderFile(string slug, int id, int id2) { User user = Users.GetLoggedInUser(); UserTaskData userTask = db.UserTasks.SingleOrDefault(utd => utd.UserID == id2 && utd.TaskID == id); if (userTask != null) { // is there a file? Guid uniqueID = Guid.NewGuid(); string originalFilename = ""; string originalExtension = ""; int fileID = 0; HttpPostedFileBase httpFile = null; // save file if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) { httpFile = Request.Files[0]; originalFilename = Path.GetFileName(httpFile.FileName); originalExtension = Path.GetExtension(httpFile.FileName); string filePath = Path.Combine(Settings.GradedFilesLocation, uniqueID.ToString() + originalExtension); httpFile.SaveAs(filePath); GradedFile gradedFile = new GradedFile(); gradedFile.UserID = user.UserID; gradedFile.UniqueID = uniqueID; gradedFile.ContentType = httpFile.ContentType; gradedFile.Length = httpFile.ContentLength; gradedFile.Filename = originalFilename; gradedFile.UploadedDate = DateTime.Now; // save the file to the database db.GradedFiles.Add(gradedFile); db.SaveChanges(); // update teh File ID and grader userid userTask.GraderUserID = user.UserID; userTask.GraderFileID = gradedFile.FileID; db.SaveChanges(); } } // when a file is uploaded return RedirectToAction("EnterTaskGrades", new { slug = slug, id = id }); }
public ActionResult MassUpload(string slug, int id) { if (Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) { User user = Users.GetLoggedInUser(); HttpPostedFileBase httpFile = Request.Files[0]; Guid tempID = Guid.NewGuid(); string tempPath = Path.Combine(Settings.GradedFilesLocation, tempID.ToString()); httpFile.SaveAs(tempPath); ZipFile zipFile = ZipFile.Read(tempPath); foreach (ZipEntry e in zipFile) { if (e.FileName.Contains("__MACOSX")) continue; // get task and student ID string gradedFilename = e.FileName; int userID = 0; int taskID = 0; int gradeNumber = 0; Match match = Regex.Match(gradedFilename, @"\[(\d+),(\d+)(,(\d+))?\]"); if (match.Success && Int32.TryParse(match.Groups[1].Value, out taskID) && Int32.TryParse(match.Groups[2].Value, out userID)) { UserTaskData userData = db.UserTasks.SingleOrDefault(utd => utd.UserID == userID && utd.TaskID == taskID); Guid uniqueID = Guid.NewGuid(); string originalExtension = Path.GetExtension(gradedFilename); string filePath = Path.Combine(Settings.GradedFilesLocation, uniqueID.ToString() + originalExtension); // create a new grader file GradedFile gradedFile = new GradedFile(); gradedFile.UserID = (user != null) ? user.UserID : 0; gradedFile.UniqueID = uniqueID; gradedFile.ContentType = httpFile.ContentType; gradedFile.Length = httpFile.ContentLength; gradedFile.Filename = gradedFilename; gradedFile.UploadedDate = DateTime.Now; db.GradedFiles.Add(gradedFile); db.SaveChanges(); // record to task userData.GraderFileID = gradedFile.FileID; userData.GraderSubmitDate = DateTime.Now; // test for grade number if (Int32.TryParse(match.Groups[4].Value, out gradeNumber)) { userData.NumericGrade = gradeNumber; } db.SaveChanges(); // save this file? FileStream fileStream = new FileStream(filePath, FileMode.CreateNew); e.Extract(fileStream); fileStream.Close(); } } } return RedirectToAction("EnterTaskGrades", new { slug = slug, id = id }); }