Initial commit - Common.RCL

This commit is contained in:
2026-08-05 21:16:11 +03:00
commit c6b8aea543
150 changed files with 5817 additions and 0 deletions
+142
View File
@@ -0,0 +1,142 @@
@inject HttpClient httpClient
<h3>Multiple File Selection</h3>
<InputFile multiple OnChange="@HandleFilesSelected" accept="@AcceptedFileTypes" />
@if (ReadyToUpload.Any() || AlreadyUploaded.Any())
{
<div style="margin-top: 20px;">
<h4>Selected Files:</h4>
<ul>
@foreach (var file in AlreadyUploaded)
{
<div class="flex flex-row">
<li>@file</li>
<button type="button" class="btn"
@onclick="() => {ReadyToDeleteFromDB.Add(file); AlreadyUploaded.Remove(file);}">X</button>
</div>
}
@foreach (var file in ReadyToUpload)
{
<div class="flex flex-row">
<li>@file.Name</li>
<button type="button" class="btn" @onclick="() => ReadyToUpload.Remove(file)">X</button>
</div>
}
</ul>
@* <button class="btn btn-primary" @onclick="UploadFiles">Upload Selected Files</button> *@
</div>
}
@if (!string.IsNullOrEmpty(message))
{
<p>@message</p>
}
@code {
[Parameter]
public string UploadLink { get; set; }
[Parameter]
public string DeleteLink { get; set; }
[Parameter]
public string AcceptedFileTypes { get; set; } = "*";
[Parameter]
public List<string> AlreadyUploaded { get; set; }
List<string> ReadyToDeleteFromDB { get; set; }
private List<IBrowserFile> ReadyToUpload = new();
private const long MaxFileSize = 1024 * 1024 * 10; // 10MB
private string message = string.Empty;
public async Task<List<string>> UpdateFiles()
{
var updatedImages = new List<string>();
await DeleteFromServer();
var uploadedImages = await UploadToServer();
updatedImages.AddRange(uploadedImages);
updatedImages.AddRange(AlreadyUploaded);
return updatedImages;
}
async Task<List<string>> UploadToServer()
{
var uploadedImages = new List<string>();
try
{
foreach (var file in ReadyToUpload)
{
var content = new MultipartFormDataContent();
var fileContent = new StreamContent(file.OpenReadStream(MaxFileSize));
fileContent.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(file.ContentType);
content.Add(fileContent, "file", file.Name);
var response = await httpClient.PostAsync($"{UploadLink}", content);
uploadedImages.Add(await response.Content.ReadAsStringAsync());
}
message = $"Successfully processed {ReadyToUpload.Count} file(s)";
return uploadedImages;
// Optional: Clear selection after upload
// selectedFiles.Clear();
}
catch (Exception ex)
{
message = $"Error uploading files: {ex.Message}";
return uploadedImages;
}
}
async Task DeleteFromServer()
{
try
{
foreach (var name in ReadyToDeleteFromDB)
{
await httpClient.DeleteAsync($"{name.Split("/")[-1]}");
}
}
catch
{
}
}
private void HandleFilesSelected(InputFileChangeEventArgs e)
{
ReadyToUpload.Clear();
// Get all selected files
foreach (var file in e.GetMultipleFiles())
{
// Optional: Add file size limit (e.g., 5MB)
if (file.Size <= 5 * 1024 * 1024)
{
ReadyToUpload.Add(file);
}
}
message = $"Selected {ReadyToUpload.Count} file(s)";
}
}