Files
Moonlight/Moonlight/Shared/Components/Forms/ChatFileSelect.razor

56 lines
1.3 KiB
Plaintext

@using Microsoft.AspNetCore.Components.Forms
@inject ToastService ToastService
@{
var id = $"fileUpload{GetHashCode()}";
}
<InputFile OnChange="OnFileChanged" type="file" id="@id" hidden=""/>
@if (SelectedFile != null)
{
<button @onclick="RemoveSelection" class="btn btn-icon btn-bg-light btn-color-danger rounded-start rounded-end">
<i class="bx bx-sm bx-x"></i>
</button>
}
else
{
<label for="@id" class="btn btn-icon btn-bg-light btn-color-primary rounded-start rounded-end">
<i class="bx bx-sm bx-upload"></i>
</label>
}
@code
{
[Parameter]
public IBrowserFile? SelectedFile { get; set; }
[Parameter]
public int MaxFileSize { get; set; } = 1024 * 1024 * 5;
private async Task OnFileChanged(InputFileChangeEventArgs arg)
{
if (arg.FileCount > 0)
{
if (arg.File.Size < MaxFileSize)
{
SelectedFile = arg.File;
await InvokeAsync(StateHasChanged);
return;
}
await ToastService.Danger($"The uploaded file should not be bigger than {Formatter.FormatSize(MaxFileSize)}");
}
SelectedFile = null;
await InvokeAsync(StateHasChanged);
}
public async Task RemoveSelection()
{
SelectedFile = null;
await InvokeAsync(StateHasChanged);
}
}