-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Closed
Labels
✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Resolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsquestion
Description
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)
Further technical details
- I'm using the VS windows.
- Also using the Dot net 8.0.
Metadata
Metadata
Assignees
Labels
✔️ Resolution: AnsweredResolved because the question asked by the original author has been answered.Resolved because the question asked by the original author has been answered.Status: Resolvedarea-blazorIncludes: Blazor, Razor ComponentsIncludes: Blazor, Razor Componentsquestion