-
Notifications
You must be signed in to change notification settings - Fork 81
Added new kb article fileselect-blazor-initial-files #2897
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
xristianstefanov
merged 6 commits into
master
from
new-kb-fileselect-blazor-initial-files-e216f80fbf754aa38b9d39996ec0e2ee
Apr 10, 2025
Merged
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
7312c8a
Added new kb article fileselect-blazor-initial-files
85034a4
Update knowledge-base/fileselect-blazor-initial-files.md
xristianstefanov 6257060
Update knowledge-base/fileselect-blazor-initial-files.md
xristianstefanov a85841b
Update knowledge-base/fileselect-blazor-initial-files.md
xristianstefanov d579a6f
Update knowledge-base/fileselect-blazor-initial-files.md
xristianstefanov 960bcd3
Update knowledge-base/fileselect-blazor-initial-files.md
xristianstefanov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,124 @@ | ||
| --- | ||
| title: Initial Files not Displaying in FileSelect | ||
| description: Learn how to show initial files in the FileSelect component for Blazor. | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| type: troubleshooting | ||
| page_title: How to Display Initial Files in Blazor FileSelect Component | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| slug: fileselect-kb-blazor-initial-files | ||
| tags: fileselect, blazor, initial, files | ||
| res_type: kb | ||
| ticketid: 1683091 | ||
| --- | ||
|
|
||
| ## Environment | ||
|
|
||
| <table> | ||
| <tbody> | ||
| <tr> | ||
| <td>Product</td> | ||
| <td>FileSelect for Blazor</td> | ||
| </tr> | ||
| </tbody> | ||
| </table> | ||
|
|
||
| ## Description | ||
|
|
||
| In an attempt to display initial files in the [`FileSelect`](slug:fileselect-overview) component, the files are fetched from an API call and assigned to the Files parameter of the component. Despite this, the component does not render the initial file list as expected. | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Cause | ||
|
|
||
| The root cause of the issue is that the `Files` parameter of the FileSelect component is not designed to be reactive. Consequently, changes made to this parameter at runtime do not automatically trigger the component to update or re-render with the new list of files. | ||
|
|
||
| ## Solution | ||
|
|
||
| To ensure that the FileSelect component reflects changes made to the `Files` parameter, you’ll need to recreate the FileSelect component. | ||
|
|
||
| `````Razor | ||
| @if (ShouldRenderFileSelect) | ||
| { | ||
| <TelerikFileSelect @ref="@FileSelectRef" | ||
| Class="required" | ||
| Files="@InitialFiles" | ||
| Multiple="true" | ||
| OnSelect="@OnSelectHandler" | ||
| OnRemove="@OnFileRemoved" /> | ||
| } | ||
|
|
||
| @code { | ||
| private TelerikFileSelect FileSelectRef { get; set; } | ||
| private List<FileSelectFileInfo> InitialFiles = new(); | ||
| private List<OSRFileInfoResponse> Files = new(); | ||
| private bool ShouldRenderFileSelect { get; set; } = true; | ||
|
|
||
| protected override async Task OnInitializedAsync() | ||
| { | ||
| await LoadFiles(); | ||
| } | ||
|
|
||
| private async Task LoadFiles() | ||
| { | ||
| // Simulate API call | ||
| await Task.Delay(500); | ||
| Files = await FetchFilesFromApi(); | ||
|
|
||
| if (Files != null) | ||
| { | ||
| InitialFiles = Files.Select(file => new FileSelectFileInfo | ||
| { | ||
| Id = Guid.NewGuid().ToString(), | ||
| Name = file.Name!, | ||
| Extension = Path.GetExtension(file.FileName!), | ||
| Size = file.FileSize | ||
| }).ToList(); | ||
| } | ||
|
|
||
| // Re-render the component to ensure changes are applied | ||
| ResetFileSelect(); | ||
| } | ||
|
|
||
| private void ResetFileSelect() | ||
| { | ||
| ShouldRenderFileSelect = false; | ||
| StateHasChanged(); | ||
|
|
||
| Task.Delay(1).ContinueWith(_ => | ||
| { | ||
| ShouldRenderFileSelect = true; | ||
| InvokeAsync(StateHasChanged); | ||
| }); | ||
| } | ||
|
|
||
| private Task<List<OSRFileInfoResponse>> FetchFilesFromApi() | ||
| { | ||
| // Mock API Response | ||
| return Task.FromResult(new List<OSRFileInfoResponse> | ||
| { | ||
| new OSRFileInfoResponse { FileName = "document.pdf", FileSize = 1024 * 1024, Buffer = new byte[10] }, | ||
| new OSRFileInfoResponse { FileName = "image.jpg", FileSize = 2048 * 1024, Buffer = new byte[10] } | ||
| }); | ||
| } | ||
|
|
||
| private void OnSelectHandler() | ||
| { | ||
| // OnSelect logic | ||
| } | ||
|
|
||
| private void OnFileRemoved() | ||
| { | ||
| // OnRemove logic | ||
| } | ||
|
|
||
| private void DownloadFileFromStream(byte[] buffer, string fileName) | ||
| { | ||
| // Simulate file download | ||
| } | ||
|
|
||
| public class OSRFileInfoResponse | ||
| { | ||
| public byte[]? Buffer { get; set; } | ||
| public string? FileName { get; set; } | ||
| public long FileSize { get; set; } | ||
|
|
||
| public string? Name => FileName?.Split("/").LastOrDefault(); | ||
| } | ||
| } | ||
| ````` | ||
xristianstefanov marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.