Skip to content

Unable to send large byte array from the server to the client in Blazor #62477

Open
@sathiyaseelanksf3503

Description

@sathiyaseelanksf3503

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

An exception occurred with the message 'A task was canceled' while attempting to send a PDF file's byte array from the server to the client in Blazor, after retrieving the file from the root folder.

Expected Behavior

Expected behavior get the file byte array in the client and open the byte array has PDF.

Steps To Reproduce

Below provided the replication steps with the Sample.

  • Run the sample.
  • Click the "Load Large PDF" button.
  • Exception occurred.

Exceptions (if any)

Image

.NET Version

Dotnet 8.0

Anything else?

The functionality works correctly in .NET 9.0, but the issue occurs specifically in .NET 8.0.

Code Snippet

Server-side code

@page "/"
@inject IJSRuntime JS
@inject IWebHostEnvironment Env

<button @onclick="LoadLargeFile">Load Large Pdf</button>

@code {
    private DotNetObjectReference<Home>? objRef;

    public void LoadLargeFile()
    {
        LoadPdf("200MB_File.pdf");
    }

    public async void LoadPdf(string fileName)
    {
        var filePath = Path.Combine(Env.WebRootPath, "files", fileName);
        byte[]? fileBytes = await File.ReadAllBytesAsync(filePath);
        await JS.InvokeVoidAsync("pdfInterop.receivePdfBytes", fileBytes, objRef);
        fileBytes = null;
    }
}

Client-side code

window.pdfInterop = {
    receivePdfBytes: function (byteArray, dotNetRef) {
        // Create a Blob and open as PDF
        const blob = new Blob([byteArray], { type: 'application/pdf' });
        const url = URL.createObjectURL(blob);
        window.open(url, '_blank');
    }
};

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-blazorIncludes: Blazor, Razor Components

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions