Skip to content

Commit 773d974

Browse files
kendo-botKB BotxristianstefanovTsvetomir-Hr
authored
Added new kb article fileselect-blazor-initial-files (#2897)
* Added new kb article fileselect-blazor-initial-files * Update knowledge-base/fileselect-blazor-initial-files.md Co-authored-by: Tsvetomir Hristov <[email protected]> * Update knowledge-base/fileselect-blazor-initial-files.md Co-authored-by: Tsvetomir Hristov <[email protected]> * Update knowledge-base/fileselect-blazor-initial-files.md Co-authored-by: Tsvetomir Hristov <[email protected]> * Update knowledge-base/fileselect-blazor-initial-files.md Co-authored-by: Tsvetomir Hristov <[email protected]> * Update knowledge-base/fileselect-blazor-initial-files.md Co-authored-by: Tsvetomir Hristov <[email protected]> --------- Co-authored-by: KB Bot <[email protected]> Co-authored-by: Hristian Stefanov <[email protected]> Co-authored-by: Tsvetomir Hristov <[email protected]>
1 parent 7a5bb7a commit 773d974

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

Diff for: knowledge-base/fileselect-blazor-initial-files.md

+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
---
2+
title: Initial Files Do Not Display in Blazor FileSelect
3+
description: Learn why initial files do not appear in the Blazor FileSelect component and how to display them correctly on initialization.
4+
type: troubleshooting
5+
page_title: Initial Files Do Not Display in Blazor FileSelect
6+
slug: fileselect-kb-blazor-initial-files
7+
tags: fileselect, blazor, initial, files
8+
res_type: kb
9+
ticketid: 1683091
10+
---
11+
12+
## Environment
13+
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>FileSelect for Blazor</td>
19+
</tr>
20+
</tbody>
21+
</table>
22+
23+
## Description
24+
25+
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.
26+
27+
## Cause
28+
29+
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.
30+
31+
## Solution
32+
33+
To ensure that the FileSelect component reflects changes made to the `Files` parameter, you’ll need to recreate the FileSelect component.
34+
35+
`````RAZOR
36+
@if (ShouldRenderFileSelect)
37+
{
38+
<TelerikFileSelect @ref="@FileSelectRef"
39+
Class="required"
40+
Files="@InitialFiles"
41+
Multiple="true" />
42+
}
43+
44+
@code {
45+
private TelerikFileSelect? FileSelectRef { get; set; }
46+
private List<FileSelectFileInfo> InitialFiles = new();
47+
private List<OSRFileInfoResponse> Files = new();
48+
private bool ShouldRenderFileSelect { get; set; } = true;
49+
50+
protected override async Task OnInitializedAsync()
51+
{
52+
await LoadFiles();
53+
}
54+
55+
private async Task LoadFiles()
56+
{
57+
// Simulate API call
58+
await Task.Delay(500);
59+
Files = await FetchFilesFromApi();
60+
61+
if (Files != null)
62+
{
63+
InitialFiles = Files.Select(file => new FileSelectFileInfo
64+
{
65+
Id = Guid.NewGuid().ToString(),
66+
Name = file.Name!,
67+
Extension = Path.GetExtension(file.FileName!),
68+
Size = file.FileSize
69+
}).ToList();
70+
}
71+
72+
// Re-render the component to ensure changes are applied
73+
ResetFileSelect();
74+
}
75+
76+
private void ResetFileSelect()
77+
{
78+
ShouldRenderFileSelect = false;
79+
StateHasChanged();
80+
81+
Task.Delay(1).ContinueWith(_ =>
82+
{
83+
ShouldRenderFileSelect = true;
84+
InvokeAsync(StateHasChanged);
85+
});
86+
}
87+
88+
private Task<List<OSRFileInfoResponse>> FetchFilesFromApi()
89+
{
90+
// Mock API Response
91+
return Task.FromResult(new List<OSRFileInfoResponse>
92+
{
93+
new OSRFileInfoResponse { FileName = "document.pdf", FileSize = 1024 * 1024, Buffer = new byte[10] },
94+
new OSRFileInfoResponse { FileName = "image.jpg", FileSize = 2048 * 1024, Buffer = new byte[10] }
95+
});
96+
}
97+
98+
public class OSRFileInfoResponse
99+
{
100+
public byte[]? Buffer { get; set; }
101+
public string? FileName { get; set; }
102+
public long FileSize { get; set; }
103+
104+
public string? Name => FileName?.Split("/").LastOrDefault();
105+
}
106+
}

0 commit comments

Comments
 (0)