public async Task <IActionResult> Create(MyTaskViewModel model, IFormFile[] uploadedFiles) { if (ModelState.IsValid) { TaskModelBL task = new TaskModelBL { Name = model.Name, TargetDate = model.TargetDate, Details = model.Details, IsRepeating = model.IsRepeating, Multiplier = model.Multiplier, PeriodCode = model.PeriodCode, ParentTaskId = model.ParentTaskId, files = new List <FileModelBL>() }; if (model.TaskСategoryId != 0) { task.TaskСategoryId = model.TaskСategoryId; } if (model.TaskPriorityId != 0) { task.TaskPriorityId = model.TaskPriorityId; } if (task.IsRepeating == true && task.Multiplier == null) { task.Multiplier = 1; } foreach (IFormFile uploadedFile in uploadedFiles) { using (var ms = new MemoryStream()) { uploadedFile.CopyTo(ms); var fileBytes = ms.ToArray(); FileModelBL fl = new FileModelBL { FileName = uploadedFile.FileName, ContentType = uploadedFile.ContentType, Data = fileBytes }; task.files.Add(fl); } } await _client.Post("api/mytask/create", task); return(RedirectToAction(nameof(Index))); } else { return(View(model)); } }
private string SaveFile(int taskId, FileModelBL model) { string fullPath; string fileName = $"{Regex.Replace(Path.GetFileNameWithoutExtension(model.FileName), @"[^\u0000-\u007F]+", string.Empty)}{DateTime.Now.ToString(optionsForUploadFiles.FileNameMask)}{Path.GetExtension(model.FileName)}"; string path = $"{hostingEnvironment.ContentRootPath}\\{optionsForUploadFiles.FilderForFiles}\\{taskId}\\"; if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } fullPath = path + fileName; using (var fileStream = new FileStream(fullPath, FileMode.Create)) { fileStream.Write(model.Data, 0, model.Data.Length); } return(fileName); }
public bool Create(int taskId, FileModelBL model) { string SafeFileName = SaveFile(taskId, model); MyFile fileForTask = new MyFile() { FileName = SafeFileName, ContentType = model.ContentType, TaskFiles = new List <TaskFile>() { new TaskFile() { TaskId = taskId } } }; context.Files.Add(fileForTask); context.SaveChanges(); return(true); }
public async Task <ActionResult> Update(int id, IFormFile[] uploadedFiles, [FromForm] MyTaskViewModel model) { if (ModelState.IsValid) { TaskModelBL task = new TaskModelBL() { Id = id, Name = model.Name, TargetDate = model.TargetDate, Details = model.Details, IsRepeating = model.IsRepeating, ParentTaskId = model.ParentTaskId, Multiplier = model.Multiplier, PeriodCode = model.PeriodCode }; task.files = new List <FileModelBL>(); if (model.files != null) { foreach (FileInfoViewModel fl in model.files) { if (fl.Deleted == 0) { task.files.Add(new FileModelBL() { Id = fl.Id }); } } } foreach (IFormFile uploadedFile in uploadedFiles) { using (var ms = new MemoryStream()) { uploadedFile.CopyTo(ms); var fileBytes = ms.ToArray(); FileModelBL fl = new FileModelBL { FileName = uploadedFile.FileName, ContentType = uploadedFile.ContentType, Data = fileBytes }; task.files.Add(fl); } } if (model.TaskСategoryId != 0) { task.TaskСategoryId = model.TaskСategoryId; } if (model.TaskPriorityId != 0) { task.TaskPriorityId = model.TaskPriorityId; } await _client.Put("api/mytask/update", id, task); return(RedirectToAction(nameof(Index))); } return(View(model)); }