Inferred picturefechingservice

This commit is contained in:
2020-01-07 22:43:56 +01:00
parent 383a8720a0
commit 9a48724907
3 changed files with 49 additions and 9 deletions

View File

@ -1,7 +1,9 @@
@page "/fetchdata" @page "/fetchdata"
@using Blazor3State.Data @using Blazor3State.Data
@inject WeatherForecastService ForecastService @using System.IO
@inject IHttpClientFactory httpClientFactory
<h1>Showing states in blazor</h1> <h1>Showing states in blazor</h1>
@ -14,7 +16,7 @@
<div class="lds-default"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div> <div class="lds-default"><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div><div></div></div>
</Loading> </Loading>
<Loaded> <Loaded>
<img src="https://loremflickr.com/200/300" alt="Alternate text" /> <img src="@imageData" alt="Alternate text" />
</Loaded> </Loaded>
<ErrorContent> <ErrorContent>
<p><em>An error has occurred. Click new image to try again !</em></p> <p><em>An error has occurred. Click new image to try again !</em></p>
@ -25,21 +27,58 @@
@code { @code {
Random random = new Random(); Random random = new Random();
LoadingContainerState state; LoadingContainerState state;
string imageData;
protected override async Task OnInitializedAsync() protected override async Task OnInitializedAsync()
{ {
await LoadImage(); await LoadImage();
} }
//async Task LoadImage()
//{
// state = LoadingContainerState.Loading;
// await Task.Delay(2000);
// if (random.Next(3) == 2)
// {
// state = LoadingContainerState.Error;
// }
// else state = LoadingContainerState.Loaded;
//}
async Task LoadImage() async Task LoadImage()
{ {
state = LoadingContainerState.Loading; state = LoadingContainerState.Loading;
await Task.Delay(2000); await Task.Delay(2000);
if (random.Next(3) == 2)
var request = new HttpRequestMessage(HttpMethod.Get, "https://loremflickr.com/400/500");
var client = httpClientFactory.CreateClient();
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{ {
state = LoadingContainerState.Error; using var responseStream = await response.Content.ReadAsStreamAsync();
var header = response.Content.Headers.FirstOrDefault(x => x.Key == "Content-Type");
string imageType = header.Value.First();
string encodedImage;
using (MemoryStream m = new MemoryStream())
{
responseStream.CopyTo(m);
byte[] imageBytes = m.ToArray();
encodedImage = Convert.ToBase64String(imageBytes);
} }
else state = LoadingContainerState.Loaded;
imageData = $"data:{imageType};base64,{encodedImage}";
state = LoadingContainerState.Loaded;
} }
else
state = LoadingContainerState.Error;
}
} }

View File

@ -29,6 +29,7 @@ namespace Blazor3State
services.AddRazorPages(); services.AddRazorPages();
services.AddServerSideBlazor(); services.AddServerSideBlazor();
services.AddSingleton<WeatherForecastService>(); services.AddSingleton<WeatherForecastService>();
services.AddHttpClient();
} }
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline. // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.

View File

@ -187,8 +187,8 @@ app {
justify-content: center; justify-content: center;
align-items: center; align-items: center;
background: #aaa; background: #aaa;
height: 300px; height: 500px;
width: 200px; width: 400px;
} }
.lds-default { .lds-default {