Hello,
I am trying to implement a progress bar while importing an excel file. The progress bar does not work, I implement the sample within my code and its not working anyway.
Here is the code :
@page "/administration/excel"
@inject HttpClient httpClient
@inject IJSRuntime jSRuntime
@implements IDisposable
@using System.Timers
@using XxxPathie.Libraries.Shared.Models.Tools;
@using XxxPathie.Libraries.Ui.Tools.Network;
<h3>Tools</h3>
<label for="specialites">Spécialités</label>
<InputFile id="specialites" OnChange="OnSpecialitesChange">Specialités</InputFile>
<br />
<label for="motscles">Mots clés</label>
<InputFile id="motscles" OnChange="OnMotsClesChange">Mots clés</InputFile>
<br />
<label for="principesactifs">Principes actifs</label>
<InputFile id="principesactifs" OnChange="OnPrincipesActifsChange">Principes actifs</InputFile>
<br />
<label for="preparations">Préparations</label>
<InputFile id="preparations" OnChange="OnPreparationsChange">Préparations</InputFile>
<br />
<br />
<TelerikProgressBar @ref="progressBar" Value="@progressValue" Max="progressMax"></TelerikProgressBar>
<TelerikProgressBar Value="@ProgressValue" Max="100"></TelerikProgressBar>
@code {
TelerikProgressBar progressBar;
Timer timer = new();
ExcelFile progress = new();
double progressValue = 0;
double progressMax = 1000;
bool refresh = false;
public void Dispose()
{
StopProgress();
Timer?.Close();
}
protected override void OnAfterRender(bool firstRender)
{
if (Timer.Enabled == false)
{
Timer.Start();
}
}
public void OnTimerElapsed(Object source, ElapsedEventArgs e)
{
if (ProgressValue < 100)
{
UpdateProgress();
}
else
{
StopProgress();
}
}
public void UpdateProgress()
{
ProgressValue += ProgressStep;
InvokeAsync(StateHasChanged);
}
public void StopProgress()
{
Timer?.Stop();
}
public const int TimerInterval = 1000;
public const int TotalTime = 10 * TimerInterval;
public double ProgressValue = 0;
public int ProgressStep = 100 / (TotalTime / TimerInterval);
public Timer Timer { get; set; } = new Timer();
private async void OnSpecialitesChange(InputFileChangeEventArgs args)
{
await ManageDownload(args.File, "excel/specialites", "Spécialités.xlsx");
}
private async void OnMotsClesChange(InputFileChangeEventArgs args)
{
await ManageDownload(args.File, "excel/motscles", "Mots clés.xlsx");
}
private async void OnPrincipesActifsChange(InputFileChangeEventArgs args)
{
await ManageDownload(args.File, "excel/principesactifs", "Principes actifs.xlsx");
}
private async void OnPreparationsChange(InputFileChangeEventArgs args)
{
await ManageDownload(args.File, "excel/preparations", "Préparations.xlsx");
}
private async Task ManageDownload(IBrowserFile file, string url, string fileName)
{
byte[] bytes = new byte[file.Size];
using (var stream = file.OpenReadStream(250 * 1024 * 1024))
{
await stream.ReadAsync(bytes, 0, bytes.Length);
}
progress = await RestClient.Instance.PostAsync<ExcelFile>(httpClient, url, bytes);
progressValue = 0;
progressMax = progress.MaxCount;
refresh = !refresh;
timer.Stop();
timer.Interval = 200;
timer.AutoReset = true;
timer.Elapsed -= ProgressPlease;
timer.Elapsed += ProgressPlease;
timer.Start();
}
private void ProgressPlease(Object source, ElapsedEventArgs e)
{
progressValue++;
progressMax = 1000;
}
private async Task ProgressReport(ExcelFile progress, string fileName)
{
try
{
for (; ; )
{
progress = await PoolProgress(progress, "fileName");
progressMax = progress.MaxCount;
progressValue = progress.DoneCount;
}
}
catch (Exception exception)
{
var breakime = 1;
}
}
private async Task<ExcelFile> PoolProgress(ExcelFile progress, string fileName)
{
progress = await RestClient.Instance.GetAsync<ExcelFile>(httpClient, "excel/progress/" + progress.Identifier);
if (progress.DoneCount == progress.MaxCount && progress.bytes != null)
{
var href = httpClient.BaseAddress + progress.Identifier.ToString();
await jSRuntime.InvokeAsync<object>("triggerFileDownload", fileName, href);
throw new Exception("OK");
}
else if (progress.Identifier == Guid.Empty)
{
return progress;
}
else if (progress.DoneCount == progress.MaxCount)
{
throw new Exception("OK");
}
return progress;
}
}