From 999a4b216dd8d99d8b340346574b97b08d332d6a Mon Sep 17 00:00:00 2001 From: Hamid Reza Mohammadi Date: Thu, 10 Jun 2021 20:41:31 +0430 Subject: [PATCH] #128 changing poet image is now working --- GanjooRazor/Areas/Admin/Pages/Poet.cshtml | 13 +++++- GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs | 48 ++++++++++++++++++-- 2 files changed, 55 insertions(+), 6 deletions(-) diff --git a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml index d94e423c..bbe613a2 100644 --- a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml +++ b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml @@ -15,7 +15,7 @@

} -
+ @@ -49,10 +49,19 @@
- + + + +
+ @Model.Poet.Name
تصویر جدید: + + + + +
diff --git a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs index b0efd758..14b92059 100644 --- a/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs +++ b/GanjooRazor/Areas/Admin/Pages/Poet.cshtml.cs @@ -1,8 +1,10 @@ using System.Collections.Generic; +using System.IO; using System.Net.Http; using System.Text; using System.Threading.Tasks; using GanjooRazor.Utils; +using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc.RazorPages; using Microsoft.Extensions.Caching.Memory; @@ -44,9 +46,12 @@ namespace GanjooRazor.Areas.Admin.Pages [BindProperty] public GanjoorPoetViewModel Poet { get; set; } - public async Task OnGetAsync() + [BindProperty] + public IFormFile Image { get; set; } + + + private async Task PreparePoet() { - LastResult = ""; var response = await _httpClient.GetAsync($"{APIRoot.Url}/api/ganjoor/poet/{Request.Query["id"]}"); response.EnsureSuccessStatusCode(); @@ -55,12 +60,16 @@ namespace GanjooRazor.Areas.Admin.Pages Poet = poet.Poet; + } - + public async Task OnGetAsync() + { + LastResult = ""; + await PreparePoet(); return Page(); } - public async Task OnPostAsync() + public async Task OnPostEditPoetAsync(GanjoorPoetViewModel Poet) { LastResult = ""; using (HttpClient secureClient = new HttpClient()) @@ -83,8 +92,39 @@ namespace GanjooRazor.Areas.Admin.Pages LastResult = "ویرایش انجام شد."; + await PreparePoet(); + return Page(); } } + + public async Task OnPostUploadImageAsync(IFormFile Image) + { + LastResult = ""; + await PreparePoet(); + + + using (HttpClient secureClient = new HttpClient()) + { + await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response); + + MultipartFormDataContent form = new MultipartFormDataContent(); + + using (MemoryStream stream = new MemoryStream()) + { + await Image.CopyToAsync(stream); + var fileContent = stream.ToArray(); + form.Add(new ByteArrayContent(fileContent, 0, fileContent.Length), Poet.Nickname, Poet.ImageUrl); + + HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/ganjoor/poet/image/{Request.Query["id"]}", form); + response.EnsureSuccessStatusCode(); + + LastResult = "تصویر بارگذاری شد."; + + } + } + + return Page(); + } } }