Blazor Server 與 WebAssembly:對比
This article was translated from English: Does it need improvement?
TranslatedView the article in English
IronPDF 是否支援 Blazor Server 和 Blazor WebAssembly (WASM)?
IronPDF 支援 Blazor Server,但不支援 Blazor WebAssembly (WASM)。
要在 Blazor Server 中儲存 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");
}
}@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");
}
}'INSTANT VB TODO TASK: The following line could not be converted:
page "/pdfdownload" inject IJSRuntime JSRuntime * A button [to] download the PDF * <button onclick="DownloadPDF"> Download PDF</button> code
If True Then
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' 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");
' }
End If$vbLabelText $csharpLabel
以下是用於處理 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><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 伺服器教學: Blazor 伺服器教學
準備好開始了嗎?
Nuget 下載 16,685,821 | 版本: 2025.12 剛發表






