Blazor Server 與 WebAssembly:比較

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

IronPDF 是否支持 Blazor Server 和 Blazor WebAssembly (WASM)?

IronPDF 支持 Blazor Server,但不支持 Blazor WebAssembly (WASM)。

要在 Blazor Server 中儲存 PDF,您需要將 PDF 文件流轉換為位元組陣列,然後將其傳遞給 JavaScript 函式以方便下載。

以下是一個範例,說明如何在 Blazor Server 應用程式中將 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 函式。 請確保將此納入您的 _Host.cshtml 或 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 Server 允許伺服器端操作,並且可以直接在伺服器上執行 C# 程式碼。
  • Blazor WebAssembly 執行於使用者端,無法直接存取如 IronPDF 的伺服器端資源。

您可以在我們的網站上查看完整的 Blazor Server 教學:Blazor Server 教學

Curtis Chau擁有Carleton大學的電腦科學學士學位,專精於前端開發,擁有Node.js、TypeScript、JavaScript和React的專業知識。Curtis熱衷於建立直觀且美觀的使用者介面,喜愛使用現代框架並建立結構良好、視覺吸引力的手冊。

閱讀更多

準備開始了嗎?

Nuget Downloads 21,916,531版本:2026.11剛剛發布