|
| 1 | +--- |
| 2 | +title: Events |
| 3 | +page_title: FileSelect - Events |
| 4 | +description: Events in the FileSelect for Blazor. |
| 5 | +slug: fileselect-events |
| 6 | +tags: telerik,blazor,upload,async,events |
| 7 | +published: true |
| 8 | +position: 20 |
| 9 | +--- |
| 10 | + |
| 11 | +# FileSelect Events |
| 12 | + |
| 13 | +This article explains the events available in the Telerik FileSelect for Blazor: |
| 14 | + |
| 15 | +* [OnSelect](#onselect) |
| 16 | +* [OnRemove](#onremove) |
| 17 | + |
| 18 | +## OnSelect |
| 19 | + |
| 20 | +The `OnSelect` fires when one or multiple files have been selected through the `Select files` button. Contains a list of fileInfo objects, allowing processing of the files. |
| 21 | + |
| 22 | +The event handler receives a `FileSelectEventArgs` object. Its `Files` field is a collection of `FileSelectFileInfo` objects. They describe each selected file and allow its processing. |
| 23 | + |
| 24 | +The `FileSelectFileInfo` object contains the following properties: |
| 25 | + |
| 26 | +Property | Type | Description |
| 27 | +---------|----------|--------- |
| 28 | +`Id` | `string` | the unique identifier of the file. |
| 29 | +`Name`|`string` | the file name. |
| 30 | +`Size` |`long` | the file size in bytes. |
| 31 | +`Extension` |`string` | the file extension. |
| 32 | +`InvalidExtension` | `bool` | a boolean flag indicating whether the file has an extension that is not within the specified ones. |
| 33 | +`InvalidMinFileSize` | `bool` | a boolean flag indicating whether the file has a size below the minimum. |
| 34 | +`InvalidMaxFileSize` | `bool` | a boolean flag indicating whether the file exceeds the max file size. |
| 35 | +`Stream`| `FileInfoStream` | a stream that can be used to upload the file to memory, file system or other. It's used to asynchronously get the byte array data of the file. |
| 36 | + |
| 37 | +>caption Handle the OnSelect event of the FileSelect |
| 38 | +
|
| 39 | +````CSHTML |
| 40 | +*Handle the OnSelect event of the FileSelect to access the selected files and upload them*@ |
| 41 | +
|
| 42 | +@using System.IO |
| 43 | +@using Microsoft.AspNetCore.Hosting |
| 44 | +@using System.Threading |
| 45 | +@using Telerik.Blazor.Components.FileSelect |
| 46 | +
|
| 47 | +@inject IWebHostEnvironment HostingEnvironment |
| 48 | +
|
| 49 | +<div style="width:300px"> |
| 50 | + <TelerikFileSelect OnSelect=@HandleFiles |
| 51 | + AllowedExtensions="@AllowedExtensions"> |
| 52 | + </TelerikFileSelect> |
| 53 | + <div class="k-form-hint"> |
| 54 | + Expected files: <strong>JPG, PNG, GIF</strong> |
| 55 | + </div> |
| 56 | +</div> |
| 57 | +
|
| 58 | +@code { |
| 59 | + public List<string> AllowedExtensions { get; set; } = new List<string>() { ".jpg", ".png", ".gif" }; |
| 60 | + public Dictionary<string, CancellationTokenSource> Tokens { get; set; } = new Dictionary<string, CancellationTokenSource>(); |
| 61 | +
|
| 62 | + private void HandleFiles(FileSelectEventArgs args) |
| 63 | + { |
| 64 | + foreach (var file in args.Files) |
| 65 | + { |
| 66 | + if (!file.InvalidExtension) |
| 67 | + { |
| 68 | + _ = UploadFile(file); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | +
|
| 73 | + private async Task UploadFile(FileSelectFileInfo file) |
| 74 | + { |
| 75 | + Tokens.Add(file.Id, new CancellationTokenSource()); |
| 76 | + var path = Path.Combine(HostingEnvironment?.WebRootPath, file.Name); |
| 77 | + await using FileStream fs = new FileStream(path, FileMode.Create); |
| 78 | + await file.Stream.CopyToAsync(fs, Tokens[file.Id].Token); |
| 79 | + } |
| 80 | +} |
| 81 | +```` |
| 82 | + |
| 83 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 84 | + |
| 85 | + |
| 86 | +## OnRemove |
| 87 | + |
| 88 | +The `OnRemove` fires when a file has been removed from the list of selected files (by clicking the `x` icon or pressing `Del` key). Contains the removed fileInfo object. |
| 89 | + |
| 90 | +The event handler receives a `FileSelectEventArgs` object. Its `Files` field is a collection of `FileSelectFileInfo` objects. As the FileSelect component allows deleting one item at a time, the collection contains only one `FileSelectFileInfo` object (the deleted one) and it has the following fields: |
| 91 | + |
| 92 | +Property | Type | Description |
| 93 | +---------|----------|--------- |
| 94 | +`Id`| `string` | the unique identifier of the file. |
| 95 | +`Name` | `string` | the file name. |
| 96 | +`Size` | `long` | the file size in bytes. |
| 97 | +`Extension` | `string` | the file extension. |
| 98 | +`InvalidExtension` | `bool` | a boolean flag indicating whether the file has an extension that is not within the specified ones. |
| 99 | +`InvalidMinFileSize` | `bool` | a boolean flag indicating whether the file has a size below the minimum. |
| 100 | +`InvalidMaxFileSize` | `bool` | a boolean flag indicating whether the file exceeds the max file size. |
| 101 | +`Stream` | `FileInfoStream` | a stream that can be used to upload the file to memory, file system or other. It's used to asynchronously get the byte array data of the file. |
| 102 | + |
| 103 | +>caption Handle the OnRemove event of the FileSelect |
| 104 | +
|
| 105 | +````CSHTML |
| 106 | +@*Handle the OnRemove event of the FileSelect to access and delete the uploaded files*@ |
| 107 | +
|
| 108 | +@using System.IO |
| 109 | +@using Microsoft.AspNetCore.Hosting |
| 110 | +@using System.Threading |
| 111 | +@using Telerik.Blazor.Components.FileSelect |
| 112 | +
|
| 113 | +@inject IWebHostEnvironment HostingEnvironment |
| 114 | +
|
| 115 | +<div style="width:300px"> |
| 116 | + <TelerikFileSelect OnRemove=@HandleRemoveFiles |
| 117 | + AllowedExtensions="@AllowedExtensions"> |
| 118 | + </TelerikFileSelect> |
| 119 | + <div class="k-form-hint"> |
| 120 | + Expected files: <strong>JPG, PNG, GIF</strong> |
| 121 | + </div> |
| 122 | +</div> |
| 123 | +
|
| 124 | +@code { |
| 125 | + public List<string> AllowedExtensions { get; set; } = new List<string>() { ".jpg", ".png", ".gif" }; |
| 126 | + public Dictionary<string, CancellationTokenSource> Tokens { get; set; } = new Dictionary<string, CancellationTokenSource>(); |
| 127 | +
|
| 128 | + private async Task HandleRemoveFiles(FileSelectEventArgs args) |
| 129 | + { |
| 130 | + foreach (var file in args.Files) |
| 131 | + { |
| 132 | + // If you're still uploading the file, cancel the process first. |
| 133 | + Tokens[file.Id].Cancel(); |
| 134 | + Tokens.Remove(file.Id); |
| 135 | +
|
| 136 | + await Task.Delay(1); |
| 137 | +
|
| 138 | + var path = Path.Combine(HostingEnvironment?.WebRootPath, file.Name); |
| 139 | + |
| 140 | + // Remove the file from the file system |
| 141 | + File.Delete(path); |
| 142 | + } |
| 143 | +
|
| 144 | + } |
| 145 | +} |
| 146 | +```` |
| 147 | + |
| 148 | +@[template](/_contentTemplates/common/general-info.md#event-callback-can-be-async) |
| 149 | + |
| 150 | + |
| 151 | +## See Also |
| 152 | + |
| 153 | +* [Live Demo: FileSelect Events](https://demos.telerik.com/blazor-ui/fileselect/events) |
| 154 | +* [FileSelect Overview]({%slug fileselect-overview%}) |
| 155 | +* [FileSelect Validation]({%slug fileselect-validation%}) |
0 commit comments