New to Telerik UI for Blazor? Start a free 30-day trial
FileSelect Stream throws NotImplementedException
Updated over 6 months ago
Environment
| Product | FileSelect for Blazor |
Description
The file.Stream object in the FileSelect OnSelect event handler throws a NotImplementedException.
The FileSelect Stream (FileInfoStream) has exposed methods that are "not implemented".
Error Message
C#
System.NotImplementedException: The method or operation is not implemented.
at Telerik.Blazor.Components.FileSelect.Stream.FileInfoStream.Read()
The same exception will occur for the following methods and properties:
PositionFlush()Read()Seek()SetLength()Write()
Possible Cause
Due to Blazor framework limitations, FileInfoStream does not support synchronous operations such as Read, Seek, Flush and Write. The methods exist, but throw an exception.
Solution
Copy the FileInfoStream asynchronously to another Stream via CopyToAsync(). Apart from the example below, also check the FileSelect OnSelect event documentation.
Copy the FileSelect Stream to another one and use sync methods
@using System.IO
<TelerikFileSelect OnSelect="@ReadSelectedFiles" />
@code {
private async Task ReadSelectedFiles(FileSelectEventArgs args)
{
foreach (var file in args.Files)
{
var ms = new MemoryStream();
await file.Stream.CopyToAsync(ms);
var byteArray = new byte[file.Size];
ms.Seek(0, SeekOrigin.Begin); // not supported by file.Stream
ms.Read(byteArray); // not supported by file.Stream
}
}
}