public IActionResult AddDocFile(string returnUrl, int PageID) { var model = new AddDocFileViewModel { ReturnUrl = returnUrl, PageID = PageID }; return(View(model)); }
public IActionResult AddDocFile(AddDocFileViewModel model) { if (ModelState.IsValid) { if (Files.AllFiles.Any(x => x.FileName == model.File.FileName)) { ModelState.AddModelError("", "File with name " + model.File.FileName + " is alredy exist."); return(View(model)); } var directory = Directory.GetFiles(environment.ContentRootPath + @"/wwwroot/FileExtansions"); var ext = new String(model.File.FileName.TakeLast(model.File.FileName.Length - (model.File.FileName.LastIndexOf('.') == -1 ? model.File.FileName.Length : model.File.FileName.LastIndexOf('.')) - 1).ToArray()); if (!directory.Any(x => x == ("File" + ext + ".png"))) { ext = ""; } var File = new DocFile() { FileName = model.File.FileName, FileExtansion = ext, DocPageId = model.PageID, Description = model.Desription }; using (var reader = new BinaryReader(model.File.OpenReadStream())) { byte[] data = reader.ReadBytes((int)model.File.Length); File.FileSize = String.Format(new FileSizeFormatProvider(), "{0:fs}", data.LongLength); File.Value = data; } Files.AddFile(File); return(LocalRedirect(model.ReturnUrl)); } return(View(model)); }