docs: #275 calling upload image api

This commit is contained in:
Hamid Reza Mohammadi 2022-02-13 20:35:52 +03:30
parent 4a6a940785
commit cb0ba49409
3 changed files with 50 additions and 3 deletions

View File

@ -80,7 +80,13 @@
</head>
<body>
<div id="hdr">
@if(!string.IsNullOrEmpty(Model.LastError))
{
@Model.LastError
}
else
{
<div id="hdr">
<h1>
@if (Model.Poet == null)
{
@ -297,6 +303,8 @@
<partial name="_FooterPartial" model="null" />
</div>
}
</body>
</html>

View File

@ -7,6 +7,7 @@ using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using RMuseum.Models.Ganjoor.ViewModels;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Text;
@ -150,6 +151,45 @@ namespace GanjooRazor.Pages
};
}
public async Task<IActionResult> OnPostAsync(PoetPhotoSuggestionUploadModel Upload)
{
LastError = "";
using (HttpClient secureClient = new HttpClient())
{
if (await GanjoorSessionChecker.PrepareClient(secureClient, Request, Response))
{
MultipartFormDataContent form = new MultipartFormDataContent();
using (MemoryStream stream = new MemoryStream())
{
form.Add(new StringContent(Upload.PoetId.ToString()), "poetId");
form.Add(new StringContent(Upload.Title), "title");
form.Add(new StringContent(Upload.Description), "description");
form.Add(new StringContent(""), "srcUrl");
await Upload.Image.CopyToAsync(stream);
var fileContent = stream.ToArray();
form.Add(new ByteArrayContent(fileContent, 0, fileContent.Length), Upload.Image.FileName, Upload.Image.FileName);
HttpResponseMessage response = await secureClient.PostAsync($"{APIRoot.Url}/api/poetphotos", form);
if (!response.IsSuccessStatusCode)
{
LastError = JsonConvert.DeserializeObject<string>(await response.Content.ReadAsStringAsync());
}
}
}
else
{
LastError = "لطفا از گنجور خارج و مجددا به آن وارد شوید.";
}
}
return Page();
}
public PhotosModel(HttpClient httpClient) : base(httpClient)
{

View File

@ -72,7 +72,6 @@ namespace RMuseum.Services.Implementation
{
return new RServiceResult<GanjoorPoetSuggestedPictureViewModel>(null, imageRes.ExceptionString);
}
await _context.SaveChangesAsync();
try
{
var image = imageRes.Result;
@ -83,7 +82,7 @@ namespace RMuseum.Services.Implementation
{
PoetId = poetId,
PicOrder = picOrder,
PictureId = image.Id,
Picture = image,
SuggestedById = userId,
Published = false,
ChosenOne = false