|
| 1 | +--- |
| 2 | +title: FileSelect Stream throws NotImplementedException |
| 3 | +description: FileSelect Stream (FileInfoStream) methods throw Not Implemented exceptions |
| 4 | +type: troubleshooting |
| 5 | +page_title: FileSelect Stream throws NotImplementedException |
| 6 | +slug: fileselect-kb-stream-exception |
| 7 | +position: |
| 8 | +tags: fileselect, exception |
| 9 | +ticketid: 1551288, 1552410, 1560275, 1585580 |
| 10 | +res_type: kb |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +<table> |
| 16 | + <tbody> |
| 17 | + <tr> |
| 18 | + <td>Product</td> |
| 19 | + <td>FileSelect for Blazor</td> |
| 20 | + </tr> |
| 21 | + </tbody> |
| 22 | +</table> |
| 23 | + |
| 24 | + |
| 25 | +## Description |
| 26 | + |
| 27 | +The `file.Stream` object in the [FileSelect `OnSelect` event handler]({%slug fileselect-events%}#onselect) throws a `NotImplementedException`. |
| 28 | + |
| 29 | +The FileSelect `Stream` (`FileInfoStream`) has exposed methods that are "not implemented". |
| 30 | + |
| 31 | +## Error Message |
| 32 | + |
| 33 | +>warning System.NotImplementedException: The method or operation is not implemented. |
| 34 | +> |
| 35 | +at Telerik.Blazor.Components.FileSelect.Stream.FileInfoStream.Read() |
| 36 | + |
| 37 | +The same exception will occur for the following methods and properties: |
| 38 | + |
| 39 | +* `Position` |
| 40 | +* `Flush()` |
| 41 | +* `Read()` |
| 42 | +* `Seek()` |
| 43 | +* `SetLength()` |
| 44 | +* `Write()` |
| 45 | + |
| 46 | +## Cause\Possible Cause(s) |
| 47 | + |
| 48 | +Due to Blazor framework limitations, [`FileInfoStream` does not support synchronous operations]({%slug fileselect-events%}#fileselectfileinfo) such as `Read`, `Seek`, `Flush` and `Write`. The methods exist, but throw an exception. |
| 49 | + |
| 50 | +## Solution |
| 51 | + |
| 52 | +Copy the `FileInfoStream` **asynchronously** to another `Stream` via `CopyToAsync()`. Apart from the example below, also check the [FileSelect `OnSelect` event documentation]({%slug fileselect-events%}#onselect). |
| 53 | + |
| 54 | +>caption Copy the FileSelect Stream to another one and use sync methods |
| 55 | +
|
| 56 | +```CSHTML |
| 57 | +@using System.IO |
| 58 | +
|
| 59 | +<TelerikFileSelect OnSelect="@ReadSelectedFiles" /> |
| 60 | +
|
| 61 | +@code { |
| 62 | + private async Task ReadSelectedFiles(FileSelectEventArgs args) |
| 63 | + { |
| 64 | + foreach (var file in args.Files) |
| 65 | + { |
| 66 | + var ms = new MemoryStream(); |
| 67 | + await file.Stream.CopyToAsync(ms); |
| 68 | +
|
| 69 | + var byteArray = new byte[file.Size]; |
| 70 | +
|
| 71 | + ms.Seek(0, SeekOrigin.Begin); // not supported by file.Stream |
| 72 | + ms.Read(byteArray); // not supported by file.Stream |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | +```` |
| 77 | +
|
| 78 | +## See Also |
| 79 | +
|
| 80 | +* [FileSelect Overview]({%slug fileselect-overview%}) |
0 commit comments