Skip to main content
All docs
V25.1
  • IFileInputSelectedFile.OpenReadStream(Decimal, CancellationToken) Method

    Opens a stream to read the file’s content.

    Namespace: DevExpress.Blazor

    Assembly: DevExpress.Blazor.v25.1.dll

    NuGet Package: DevExpress.Blazor

    Declaration

    Stream OpenReadStream(
        decimal maxAllowedSize = 512000M,
        CancellationToken cancellationToken = default(CancellationToken)
    )

    Optional Parameters

    Name Type Default Description
    maxAllowedSize Decimal 512000

    The stream’s maximum size in bytes.

    cancellationToken CancellationToken null

    An object that propagates a cancellation notification.

    Returns

    Type Description
    Stream

    A stream that allows you to read file content.

    Remarks

    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:

    • In Interactive WebAssembly mode, the component streams file data directly to razor code in the user browser.
    • In Interactive Server mode, the component streams file data from the client over the SignalR connection to the server’s razor code.

    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:

    • The file’s length exceeds the maxAllowedSize parameter value.
    • The file’s upload process was canceled during the method execution.

    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