Blazor Server 與 WebAssembly:對比
This article was translated from English: Does it need improvement?
Translated
View 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 下載 18,560,885 | 版本: 2026.4 剛剛發布

