diff --git a/SqlDatabaseVectorSearch/Components/Pages/Documents.razor b/SqlDatabaseVectorSearch/Components/Pages/Documents.razor index c927cac..6648044 100644 --- a/SqlDatabaseVectorSearch/Components/Pages/Documents.razor +++ b/SqlDatabaseVectorSearch/Components/Pages/Documents.razor @@ -13,7 +13,7 @@ Upload new document - +
@@ -24,7 +24,7 @@ - +
@@ -35,13 +35,13 @@ Document ID - +
- +
- +
@@ -107,18 +107,11 @@ else private bool isLoading = true; private IList documents = []; - private UploadDocumentRequest uploadDocumentRequest = new(); - private EditContext? editContext; + private UploadDocument Model { get; set; } = new(); [Inject] protected ToastService ToastService { get; set; } = default!; - protected override void OnInitialized() - { - editContext = new EditContext(uploadDocumentRequest); - base.OnInitialized(); - } - protected override async Task OnAfterRenderAsync(bool firstRender) { if (!firstRender) @@ -156,31 +149,26 @@ else } } - private void HandleFileSelected(InputFileChangeEventArgs e) - { - uploadDocumentRequest.File = e.File; - } - private async Task HandleValidSubmit() { - if (uploadDocumentRequest.File is null) + if (Model.File is null) { return; } uploadButton.ShowLoading(); - var fileName = uploadDocumentRequest.File.Name; + var fileName = Model.File.Name; try { - await using var inputStream = uploadDocumentRequest.File.OpenReadStream(20 * 1024 * 1024); // 20 MB + await using var inputStream = Model.File.OpenReadStream(20 * 1024 * 1024); // 20 MB await using var stream = await inputStream.GetMemoryStreamAsync(); await using var scope = ServiceProvider.CreateAsyncScope(); var vectorSearchService = scope.ServiceProvider.GetRequiredService(); - var documentId = string.IsNullOrWhiteSpace(uploadDocumentRequest.DocumentId) ? null : (Guid?)Guid.Parse(uploadDocumentRequest.DocumentId); + var documentId = string.IsNullOrWhiteSpace(Model.DocumentId) ? null : (Guid?)Guid.Parse(Model.DocumentId); await vectorSearchService.ImportAsync(stream, fileName, MimeUtility.GetMimeMapping(fileName), documentId); ToastService.Notify(await CreateToastMessageAsync(ToastType.Success, "Upload document", $"The document {fileName} has been successfully uploaded and indexed.")); @@ -269,9 +257,8 @@ else public string LocalCreationDateString { get; set; } = string.Empty; } - public class UploadDocumentRequest + public class UploadDocument { - [SupplyParameterFromForm] public IBrowserFile? File { get; set; } [RegularExpression(@"^(\{|\()?[0-9a-fA-F]{8}(-?)[0-9a-fA-F]{4}(-?)[0-9a-fA-F]{4}(-?)[0-9a-fA-F]{4}(-?)[0-9a-fA-F]{12}(\}|\))?$", ErrorMessage = "Invalid GUID format.")]