blazor-devexpress-dot-blazor-dot-ifileinputselectedfile-dot-openreadstream-x28-system-dot-decimal-system-dot-threading-dot-cancellationtoken-x29.md
Opens a stream to read the file’s content.
Namespace : DevExpress.Blazor
Assembly : DevExpress.Blazor.v25.2.dll
NuGet Package : DevExpress.Blazor
Stream OpenReadStream(
decimal maxAllowedSize = 512000M,
CancellationToken cancellationToken = default(CancellationToken)
)
| Name | Type | Default | Description |
|---|---|---|---|
| maxAllowedSize | Decimal | 512000 |
The stream’s maximum size in bytes.
| | cancellationToken | CancellationToken | null |
An object that propagates a cancellation notification.
|
| Type | Description |
|---|---|
| Stream |
A stream that allows you to read file content.
|
The File Input component does not upload selected files automatically. Instead, the component raises the FilesUploading event once you or users start (or restart) an upload operation.
Handle the FilesUploading event to access selected Files and call a file’s OpenReadStream method to read file content. Once the read operation is completed, you can send the file to another destination, save it to the file system, or display file content on a web page.
Based on the render mode, the File Input component streams file content in one of the following ways:
Note
Do not read a stream directly in memory to avoid performance and security-related issues. Instead, copy the stream to a file on a disk or pass file content to an external service.
The OpenReadStream method throws exceptions in the following cases:
maxAllowedSize parameter value.The following code snippet handles exceptions that can occur during upload operations:
<DxFileInput FilesUploading="OnFilesUploading" />
@code {
async Task OnFilesUploading(FilesUploadingEventArgs args) {
foreach (var file in args.Files) {
try {
/* The following code is intended for demonstration purposes only.
Do not read a stream directly in memory to avoid performance and security-related issues. */
using var stream = new System.IO.MemoryStream();
await file.OpenReadStream(4_000_000).CopyToAsync(stream);
}
catch (Exception ex) {
if (ex is IOException)
Console.WriteLine("The file length exceeds the stream size.");
if (ex is OperationCanceledException)
Console.WriteLine("The upload operation was canceled.");
}
}
}
}
Run Demo: File Input - Overview
See Also
IFileInputSelectedFile Interface