public ActionResult Edit(File file) { if (!ModelState.IsValid) { return(View("Edit", file)); } var oldFile = _context.Files.Single(n => n.Id == file.Id); oldFile.Path = file.Path; oldFile.Name = file.Name; _context.SaveChanges(); return(RedirectToAction("Index", "File")); }
public ActionResult Create(IEnumerable <HttpPostedFileBase> files) { foreach (var file in files) { if (file.ContentLength > 0) { var path = Server.MapPath(relativeServerPath); var fileName = Path.GetFileName(file.FileName); if (fileName != null) { //Erstelle Verzeichniss falls nicht existend if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } path = Path.Combine(path, fileName); file.SaveAs(path); //Erstelle einen neuen Eintrag, wenn die Datei noch nicht in der Datenbank abgelegt ist if (_context.Files.FirstOrDefault(s => s.Name == fileName) == null) { var relativePath = relativeServerPath + fileName; var dbFile = new File { Path = relativePath, Name = fileName, Owner = User.Identity.GetUserName(), Size = file.ContentLength }; _context.Files.Add(dbFile); _context.SaveChanges(); } } } } return(RedirectToAction("Index")); }