Esempio n. 1
0
        public async Task <IActionResult> Create(PerfumeAddModel 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.AddAsync(model, newName);
                }
                else
                {
                    await _perfumeApiService.AddAsync(model, null);
                }
                ViewBag.ErrorMessage = "";
                return(RedirectToAction("GetAll", "Perfume"));
            }

            ViewBag.ErrorMessage = "Sistemde aynı isme ait parfüm bulunmaktadır...!";
            return(View(model));
        }
        public async Task AddAsync(PerfumeAddModel 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 byteArrayContent = new ByteArrayContent(bytes);
                byteArrayContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(model.Image.ContentType);

                formData.Add(byteArrayContent, nameof(PerfumeAddModel.Image), model.Image.FileName);
            }
            formData.Add(new StringContent(model.PerfumeName), nameof(PerfumeAddModel.PerfumeName));
            formData.Add(new StringContent(model.BrandId.ToString()), nameof(PerfumeAddModel.BrandId));
            formData.Add(new StringContent(model.Price.ToString()), nameof(PerfumeAddModel.Price));

            await _httpClient.PostAsync("", formData);
        }