Skip to content

Commit 7312c8a

Browse files
author
KB Bot
committed
Added new kb article fileselect-blazor-initial-files
1 parent 2e32a49 commit 7312c8a

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
---
2+
title: Initial Files not Displaying in FileSelect
3+
description: Learn how to show initial files in the FileSelect component for Blazor.
4+
type: troubleshooting
5+
page_title: How to Display Initial Files in Blazor FileSelect Component
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+
OnSelect="@OnSelectHandler"
43+
OnRemove="@OnFileRemoved" />
44+
}
45+
46+
@code {
47+
private TelerikFileSelect FileSelectRef { get; set; }
48+
private List<FileSelectFileInfo> InitialFiles = new();
49+
private List<OSRFileInfoResponse> Files = new();
50+
private bool ShouldRenderFileSelect { get; set; } = true;
51+
52+
protected override async Task OnInitializedAsync()
53+
{
54+
await LoadFiles();
55+
}
56+
57+
private async Task LoadFiles()
58+
{
59+
// Simulate API call
60+
await Task.Delay(500);
61+
Files = await FetchFilesFromApi();
62+
63+
if (Files != null)
64+
{
65+
InitialFiles = Files.Select(file => new FileSelectFileInfo
66+
{
67+
Id = Guid.NewGuid().ToString(),
68+
Name = file.Name!,
69+
Extension = Path.GetExtension(file.FileName!),
70+
Size = file.FileSize
71+
}).ToList();
72+
}
73+
74+
// Re-render the component to ensure changes are applied
75+
ResetFileSelect();
76+
}
77+
78+
private void ResetFileSelect()
79+
{
80+
ShouldRenderFileSelect = false;
81+
StateHasChanged();
82+
83+
Task.Delay(1).ContinueWith(_ =>
84+
{
85+
ShouldRenderFileSelect = true;
86+
InvokeAsync(StateHasChanged);
87+
});
88+
}
89+
90+
private Task<List<OSRFileInfoResponse>> FetchFilesFromApi()
91+
{
92+
// Mock API Response
93+
return Task.FromResult(new List<OSRFileInfoResponse>
94+
{
95+
new OSRFileInfoResponse { FileName = "document.pdf", FileSize = 1024 * 1024, Buffer = new byte[10] },
96+
new OSRFileInfoResponse { FileName = "image.jpg", FileSize = 2048 * 1024, Buffer = new byte[10] }
97+
});
98+
}
99+
100+
private void OnSelectHandler()
101+
{
102+
// OnSelect logic
103+
}
104+
105+
private void OnFileRemoved()
106+
{
107+
// OnRemove logic
108+
}
109+
110+
private void DownloadFileFromStream(byte[] buffer, string fileName)
111+
{
112+
// Simulate file download
113+
}
114+
115+
public class OSRFileInfoResponse
116+
{
117+
public byte[]? Buffer { get; set; }
118+
public string? FileName { get; set; }
119+
public long FileSize { get; set; }
120+
121+
public string? Name => FileName?.Split("/").LastOrDefault();
122+
}
123+
}
124+
`````

0 commit comments

Comments
 (0)