public async Task <IActionResult> Edit(PerfumeUpdateModel model) { //string girilince value 0 alıyor if (model.Price == 0) { return(View(model)); } if (await _perfumeApiService.GetByPerfumeNameAsync(model.PerfumeName)) { if (model.Image != null) { var newName = Guid.NewGuid() + Path.GetExtension(model.Image.FileName); var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/" + newName); using (var stream = System.IO.File.Open(path, FileMode.Create, FileAccess.Write, FileShare.ReadWrite)) { await model.Image.CopyToAsync(stream); } await _perfumeApiService.UpdateAsync(model, newName); } else { await _perfumeApiService.UpdateAsync(model, null); } ViewBag.ErrorMessage = ""; return(RedirectToAction("GetAll", "Perfume")); } ViewBag.ErrorMessage = "Sistemde aynı isme ait parfüm bulunmaktadır...!"; return(View(model)); }
public async Task UpdateAsync(PerfumeUpdateModel model, string newName) { MultipartFormDataContent formData = new MultipartFormDataContent(); if (model.Image != null) { var path = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot/img/" + newName); var bytes = await System.IO.File.ReadAllBytesAsync(path); ByteArrayContent byteContent = new ByteArrayContent(bytes); byteContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(model.Image.ContentType); formData.Add(byteContent, nameof(PerfumeUpdateModel.Image), model.Image.FileName); } formData.Add(new StringContent(model.PerfumeId.ToString()), nameof(PerfumeUpdateModel.PerfumeId)); formData.Add(new StringContent(model.PerfumeName), nameof(PerfumeUpdateModel.PerfumeName)); formData.Add(new StringContent(model.BrandId.ToString()), nameof(PerfumeUpdateModel.BrandId)); formData.Add(new StringContent(model.Price.ToString()), nameof(PerfumeUpdateModel.Price)); await _httpClient.PutAsync($"{model.PerfumeId}", formData); }