public async Task <IActionResult> Create([Bind("Id,DocumentTypeId,Name,Number,ForceEntryYear,ForceEntryMonth,ForceEntryDay,File,PreviousDocumentId")] RegulatoryDocument regulatoryDocument, IFormFile DocFile) { if (ModelState.IsValid) { // не архивный при создании regulatoryDocument.Archival = false; regulatoryDocument.DeletingJustification = null; regulatoryDocument.NewDocumentId = null; regulatoryDocument.Description = null; _context.Add(regulatoryDocument); await _context.SaveChangesAsync(); // сохранение файла string sContentRootPath = _hostingEnvironment.WebRootPath; sContentRootPath = Path.Combine(sContentRootPath, "RegulatoryDocuments", regulatoryDocument.Id.ToString()); Directory.CreateDirectory(sContentRootPath); if (DocFile != null) { string path_filename = Path.Combine(sContentRootPath, Path.GetFileName(DocFile.FileName)); using (var stream = new FileStream(Path.GetFullPath(path_filename), FileMode.Create)) { await DocFile.CopyToAsync(stream); } } return(RedirectToAction(nameof(Index))); } ViewData["DocumentTypeId"] = new SelectList(_context.DocumentType, "Id", "Name", regulatoryDocument.DocumentTypeId); ViewData["PreviousDocumentId"] = new SelectList(_context.RegulatoryDocument, "Id", "Name", regulatoryDocument.PreviousDocumentId); return(View(regulatoryDocument)); }
public async Task <IActionResult> Edit(int id, [Bind("Id,DocumentTypeId,Name,Number,ForceEntryYear,ForceEntryMonth,ForceEntryDay,File,PreviousDocumentId")] RegulatoryDocument regulatoryDocument, IFormFile DocFile) { if (id != regulatoryDocument.Id) { return(NotFound()); } if (ModelState.IsValid) { try { // не архивный при редактировании regulatoryDocument.Archival = false; regulatoryDocument.DeletingJustification = null; regulatoryDocument.NewDocumentId = null; regulatoryDocument.Description = null; _context.Update(regulatoryDocument); await _context.SaveChangesAsync(); // сохранение файла string sContentRootPath = _hostingEnvironment.WebRootPath; sContentRootPath = Path.Combine(sContentRootPath, "RegulatoryDocuments", regulatoryDocument.Id.ToString()); Directory.CreateDirectory(sContentRootPath); if (DocFile != null) { // удаление всех предыдущих файлов в папке DirectoryInfo di = new DirectoryInfo(sContentRootPath); foreach (FileInfo filed in di.GetFiles()) { try { filed.Delete(); } catch { } } string path_filename = Path.Combine(sContentRootPath, Path.GetFileName(DocFile.FileName)); using (var stream = new FileStream(Path.GetFullPath(path_filename), FileMode.Create)) { await DocFile.CopyToAsync(stream); } } } catch (DbUpdateConcurrencyException) { if (!RegulatoryDocumentExists(regulatoryDocument.Id)) { return(NotFound()); } else { throw; } } return(RedirectToAction(nameof(Index))); } ViewData["DocumentTypeId"] = new SelectList(_context.DocumentType, "Id", "Name", regulatoryDocument.DocumentTypeId); ViewData["PreviousDocumentId"] = new SelectList(_context.RegulatoryDocument, "Id", "Name", regulatoryDocument.PreviousDocumentId); return(View(regulatoryDocument)); }