|
| 1 | +--- |
| 2 | +title: Initial Files |
| 3 | +page_title: FileSelect Initial Files |
| 4 | +description: The FileSelect allows you to define a collection of pre-selected files, which will be initially shown in the selected file list. |
| 5 | +slug: fileselect-initial-files |
| 6 | +tags: telerik,blazor,fileselect,initial, files |
| 7 | +published: True |
| 8 | +position: 40 |
| 9 | +--- |
| 10 | + |
| 11 | +# FileSelect Initial Files |
| 12 | + |
| 13 | +The Blazor FileSelect component enables you to display specific files in the file list when the component loads for the first time. This is a convenient way to show previously uploaded files. |
| 14 | + |
| 15 | +To configure the initially displayed files, use the `Files` parameter of the FileSelect—it accepts an `IEnumerable<FileSelectFileInfo>` collection that stores a set of pre-selected files. |
| 16 | + |
| 17 | +>caption Display initial files in FileSelect's list. |
| 18 | +
|
| 19 | +```CSHTML |
| 20 | +
|
| 21 | +<TelerikFileSelect Files="@InitialFiles" /> |
| 22 | +
|
| 23 | +@code { |
| 24 | + private List<FileSelectFileInfo> InitialFiles { get; set; } = new List<FileSelectFileInfo>() |
| 25 | + { |
| 26 | + new FileSelectFileInfo(){ Id="1", Name="Report", Extension=".pdf", Size = 1024 * 1024 * 2 }, |
| 27 | + new FileSelectFileInfo(){ Id="2", Name="Image", Extension=".jpg", Size = 1024 * 1024 * 4 }, |
| 28 | + new FileSelectFileInfo(){ Id="3", Name="Picture", Extension=".png", Size = 1024 * 1024 * 3 }, |
| 29 | + }; |
| 30 | +} |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +## Persist Selected Files |
| 35 | + |
| 36 | +The Initial Files feature of the FileSelect allows you to save a list of files that the user has selected. Then, you can display them again when the page is reloaded. To achieve this: |
| 37 | +1. Store the [`FileSelectFileInfo`]({%slug fileselect-events%}#fileselectfileinfo) records received during the [`OnSelect`]({%slug fileselect-events%}#onselect) event. |
| 38 | +1. Load the [`FileSelectFileInfo`]({%slug fileselect-events%}#fileselectfileinfo) records in the FileSelect when the page is loaded. |
| 39 | + |
| 40 | +>caption How to load files and display them initially in the FileSelect |
| 41 | +
|
| 42 | +```CSHTML |
| 43 | +
|
| 44 | +@using System.IO; |
| 45 | +
|
| 46 | +@if (InitialFiles != null) |
| 47 | +{ |
| 48 | + <TelerikFileSelect Files="@InitialFiles" |
| 49 | + OnSelect="@OnSelect" /> |
| 50 | +} |
| 51 | +
|
| 52 | +@code { |
| 53 | + private List<FileSelectFileInfo> InitialFiles { get; set; } |
| 54 | +
|
| 55 | + private void OnSelect(FileSelectEventArgs args) |
| 56 | + { |
| 57 | + foreach (var file in args.Files) |
| 58 | + { |
| 59 | + //await SaveFileInfo(file); Here, you can store the file information it in a database, text file, or any other desired storage |
| 60 | + } |
| 61 | + } |
| 62 | +
|
| 63 | + protected override async Task OnInitializedAsync() |
| 64 | + { |
| 65 | + await LoadFiles(); |
| 66 | + } |
| 67 | +
|
| 68 | + private async Task LoadFiles() |
| 69 | + { |
| 70 | + //Simulate files information loading |
| 71 | + await Task.Delay(1000); |
| 72 | + InitialFiles = new List<FileSelectFileInfo>() |
| 73 | + { |
| 74 | + new FileSelectFileInfo(){ Id="1", Name="Report", Extension=".pdf", Size = 1024 * 1024 * 2 } |
| 75 | + }; |
| 76 | + } |
| 77 | +} |
| 78 | +
|
| 79 | +``` |
| 80 | + |
| 81 | + |
| 82 | +## See Also |
| 83 | + |
| 84 | +* [FileSelect API](/blazor-ui/api/Telerik.Blazor.Components.TelerikFileSelect) |
| 85 | +* [FileSelect Overview]({%slug fileselect-overview%}) |
0 commit comments