Skip to content

Unable to transmit a large byte array from the server to the client in Blazor #63206

@sathiyaseelanksf3503

Description

@sathiyaseelanksf3503

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.

To Reproduce

To reproduce the issue, follow the below steps using this sample.

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

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');
    }
};

Exceptions (if any)

Image

Further technical details

  • I'm using the VS windows.
  • Also using the Dot net 8.0.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions