Files
Samuele Locatelli 4e533bff80 Test display pdf...
2024-02-22 18:48:53 +01:00

121 lines
3.2 KiB
C#

#if false
using CA.Blazor.Pdf;
using CA.Blazor.Pdf.Extensions;
#endif
using Microsoft.AspNetCore.Components;
using System.Net.Http.Headers;
namespace EgwCoreLib.BlazorTest.Pages
{
public partial class TestPdfViewer
{
#region Public Fields
public int CompoSel = 1;
#endregion Public Fields
#region Public Properties
public string PdfBase64 { get; set; } = "";
#endregion Public Properties
#region Protected Properties
protected string FileSel
{
get => fileSel;
set
{
if (fileSel != value)
{
fileSel = value;
ReloadData().ConfigureAwait(false);
StateHasChanged();
}
}
}
protected string HeightSel
{
get => heightSel;
set
{
if (heightSel != value)
{
heightSel = value;
StateHasChanged();
}
}
}
[Inject]
protected NavigationManager NavMan { get; set; } = null!;
#endregion Protected Properties
#region Protected Methods
protected override async Task OnInitializedAsync()
{
await ReloadData();
await Task.Delay(1);
}
#endregion Protected Methods
#region Private Properties
private string fileSel { get; set; } = "File01.pdf";
private string heightSel { get; set; } = "400px";
private string baseUri = "";
private string fileUrl = "";
private Dictionary<string, string> heightList { get; set; } = new Dictionary<string, string>() { { "200", "200px" }, { "400", "400px" }, { "600", "600px" }, { "800", "800px" }, { "1000", "1000px" } };
#endregion Private Properties
#region Private Methods
private async Task<string> GetBase64Pdf(string apiUrl)
{
using (var httpClient = new HttpClient())
{
httpClient.BaseAddress = new Uri(NavMan.BaseUri);
// Make the API request to get the PDF content
var response = await httpClient.GetAsync(apiUrl);
if (response.IsSuccessStatusCode)
{
// Convert the response content to Base64
var pdfBytes = await response.Content.ReadAsByteArrayAsync();
var base64String = Convert.ToBase64String(pdfBytes);
return $"data:application/pdf;base64,{base64String}";
}
else
{
// Handle error scenarios based on your application needs For simplicity, we
// return an empty string in case of an error
return string.Empty;
}
}
}
private async Task ReloadData()
{
baseUri = NavMan.BaseUri;
fileUrl = $"{baseUri}/test/{FileSel}";
#if false
// Call the helper method to retrieve the Base64-encoded PDF content
PdfBase64 = await GetBase64Pdf(apiUrl);
#endif
}
#endregion Private Methods
}
}