public ActionResult FileUpdate(int bookId) { var fileId = this.books.GetBookDetails(bookId).FirstOrDefault().BookFileId; var model = new BookFileUpdateModel { BookFileId = fileId, BookId = bookId }; return(this.View(model)); }
public ActionResult FileUpdate(BookFileUpdateModel model) { if (model.File != null) { int idx = model.File.FileName.LastIndexOf('.'); if (idx == -1) { this.ModelState.AddModelError(string.Empty, "File must have an extention!"); return(this.View("FileUpdate", model)); } var fileName = model.File.FileName.Substring(0, idx); var fileExtention = model.File.FileName.Substring(idx + 1); if (!Regex.IsMatch(fileExtention, ValidationConstants.FileExtentionRegex)) { this.ModelState.AddModelError(string.Empty, "File extention not supported!"); return(this.View("FileUpdate", model)); } using (var memory = new MemoryStream()) { model.File.InputStream.CopyTo(memory); var content = memory.GetBuffer(); var book = this.books.GetBookDetails(model.BookId).FirstOrDefault(); if (book.BookFileId == null) { book.BookFile = new BookFile { Downloadable = model.Downloadable, Content = content, Name = fileName, FileExtension = fileExtention }; } else { book.BookFile.Downloadable = model.Downloadable; book.BookFile.Content = content; book.BookFile.Name = fileName; book.BookFile.FileExtension = fileExtention; } var result = this.books.Update(book); } } return(this.RedirectToAction("Details", new { id = model.BookId })); }