public async Task <string> AddFileAsync(AddFileApiVm model) { string uri = this.config.UrlFileConection + "/AddFile?"; var jsonQuery = JsonConvert.SerializeObject(model); var contentSend = new StringContent(jsonQuery, Encoding.UTF8, "application/json"); HttpResponseMessage response = null; var mes = ""; try { // --> Api -> Project-> [HttpPost]Post // "https://localhost:5001/FileCrud" response = await httpClient.PostAsync(uri, contentSend); var content = response.Content; response.EnsureSuccessStatusCode(); string result = content.ReadAsStringAsync().Result; var res = JsonConvert.DeserializeObject <string>(result); mes = res; } catch (Exception ex) { mes = ex.Message; } return(mes); }
public async Task <string> AddFileAsync([FromBody] AddFileApiVm model) { var prAll = dbContext.Projects .Include(p => p.Attachments) .ToList(); var updatePr = prAll.FirstOrDefault(p => p.Id == model.ProjId); var mess = ""; if (model.File != null) { string name = model.File.FileName; string path = $"/files/{name}"; string serverPath = $"{this.environment.WebRootPath}{path}"; FileStream fs = new FileStream(serverPath, FileMode.Create, FileAccess.Write); await model.File.CopyToAsync(fs); fs.Close(); var newFile = new DbFile() { Id = Guid.NewGuid(), File = path, FileName = name, DateCreate = DateTime.Now }; dbContext.DbFiles.Add(newFile); dbContext.SaveChanges(); updatePr.Attachments.Add(newFile); updatePr.Updated = DateTime.Now; dbContext.Update(updatePr); await dbContext.SaveChangesAsync(); mess = "Файл завaнтажено!!!"; } else { mess = "Файл не вибрано!!!"; } return(mess); }
public string Post(AddFileApiVm model) { var res = apiService.AddFileAsync(model).Result; return(res); }