public async Task <IActionResult> AddFileForTaskAsync() { string taskFileLoadPath = _environment.WebRootPath + "\\Files\\" + "\\TaskFiles\\"; var detail = new OperationDetailDTO(); try { var allForms = Request.Form; StringValues taskIdString; var taskIdRes = allForms.TryGetValue(allForms.Keys.First(), out taskIdString); var strId = taskIdString.ToString(); var taskID = Convert.ToInt32(strId); var file = Request.Form.Files[0]; if (file != null) { string userFileName = file.FileName; string uniqueFileName = System.Guid.NewGuid() + userFileName; OperationDetailDTO fileRes = new OperationDetailDTO(); var currentFileRes = await _taskService.GetTaskFileNameAsync(taskID); if (currentFileRes.Succeeded) { using (var fileStream = System.IO.File.Create(taskFileLoadPath + uniqueFileName)) { file.CopyTo(fileStream); } if (System.IO.File.Exists(_environment.WebRootPath + currentFileRes.filePath)) { System.IO.File.Delete(_environment.WebRootPath + currentFileRes.filePath); } fileRes = await _taskService.UpdateTaskFileAsync(currentFileRes.fileId, userFileName, uniqueFileName); } else { using (var fileStream = System.IO.File.Create(taskFileLoadPath + uniqueFileName)) { file.CopyTo(fileStream); } fileRes = await _taskService.AddFileToTaskAsync(taskID, userFileName, uniqueFileName); } if (!fileRes.Succeeded) { if (System.IO.File.Exists(taskFileLoadPath + uniqueFileName)) { System.IO.File.Delete(taskFileLoadPath + uniqueFileName); } detail.ErrorMessages.Add("Не удалось загрузить файл к задаче."); detail.ErrorMessages.AddRange(fileRes.ErrorMessages); } else { detail.Succeeded = true; } } else { detail.ErrorMessages.Add("Файл равен null"); } return(Ok(detail)); } catch (Exception e) { detail.ErrorMessages.Add("Ошибка на сервере при загрузке файлов: " + e.Message); return(Ok(detail)); } }