Blazor Server 与 WebAssembly:比较

Curtis Chau
Curtis Chau
Updated: 2026年4月22日

IronPDF 是否支持 Blazor 服务器和 Blazor WebAssembly (WASM)?

IronPDF 支持 Blazor 服务器,但不支持 Blazor WebAssembly (WASM)。

要在 Blazor 服务器中保存 PDF,您需要将 PDF 文档流转换为字节数组,然后将其传递给 JavaScript 函数以便于下载。

以下是如何在 Blazor 服务器应用程序中将 PDF 文档转换为字节数组,然后使用 JavaScript 触发下载的示例:

@page "/pdfdownload"
@inject IJSRuntime JSRuntime

@* A button to download the PDF *@
<button @onclick="DownloadPDF">Download PDF</button>

@code {
    private async Task DownloadPDF()
    {
        // Create a PDF document using IronPDF (this is hypothetical code)
        var renderer = new IronPdf.HtmlToPdf();
        var pdfDocument = renderer.RenderHtmlAsPdf("<h1>Hello, World!</h1>");

        // Convert PDF document to a byte array
        byte[] pdfBytes = pdfDocument.BinaryData;

        // Call the JavaScript function to download the PDF
        await JSRuntime.InvokeVoidAsync("downloadFile", pdfBytes, "example.pdf");
    }
}

下面是处理 PDF 下载的 JavaScript 函数。 确保将此包含在您的index.html文件中:

<script>
    // Function to download a file from a byte array in JavaScript
    function downloadFile(fileBytes, fileName) {
        // Convert byte array to a Blob
        const blob = new Blob([fileBytes], { type: 'application/pdf' });

        // Create a link element for downloading the file
        const link = document.createElement('a');
        link.href = window.URL.createObjectURL(blob);
        link.download = fileName;

        // Append the link to the body, trigger it, and remove it afterwards
        document.body.appendChild(link); // Required for Firefox
        link.click();
        document.body.removeChild(link);
    }
</script>
JavaScript

关键点:

  • Blazor 服务器 允许进行服务器端操作,并可以直接在服务器上执行 C# 代码。
  • Blazor WebAssembly 运行在客户端,无法直接访问服务器端资源,如 IronPDF。

您可以在我们的网站上查看完整的 Blazor 服务器教程:Blazor 服务器教程

Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。

阅读更多

准备开始了吗?

Nuget Downloads 21,916,531版本:2026.11刚刚发布