IronPDF 故障排除 Blazor 服务器 / WebAssembly (WASM) Blazor Server vs. WebAssembly: Comparison Curtis Chau 更新日期:6月 1, 2025 Download IronPDF NuGet 下載 DLL 下載 Windows 安裝程式 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 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"); } } @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 以下是提供的 JavaScript 函數來處理 PDF 下載。 確保將此包括在您的_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 伺服器允許伺服器端操作,並可以直接在伺服器上執行 C# 程式碼。 Blazor WebAssembly在客戶端運行,無法直接訪問像 IronPDF 這樣的伺服器端資源。 您可以在我們的網站上查看完整的 Blazor 伺服器教程:Blazor 伺服器教程 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 16,154,058 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:16,154,058 查看許可證