Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.4k views
in Technique[技术] by (71.8m points)

problem showing PDF in Blazer page from byte array

I have gone through all the suggestions for how to take a byte array stored in SQL Server db as varbinary and display it as PDF in a Blazor website. I'm successful in ASP.Net with the aspx pages and code behind but I can't seem to find the right combination for Blazor (ShowPDF.razor and code behind ShowPDF.razor.cs)

Here is what I have as variants in the code behind:

  1. The aReport.ReportDocument is returning a byte array of aReport.DocumentSize from the DB

     FileStreamResult GetPDF()
     {
         var pdfStream = new System.IO.MemoryStream();
         this.rdb = new ReportData();
         aReport = rdb.GetWithReport(1);
    
         pdfStream.Write(aReport.ReportDocument, 0, aReport.DocumentSize);
         pdfStream.Position = 0;
         return new FileStreamResult(pdfStream, new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("application/pdf"));
     }
    

OR 2. direct binary array to base 64 encoding:

return Convert.ToBase64String(aReport.ReportDocument);

While those two processes return the data, I'm unable to find how to set up the razor page to show the result. I have tried:

<object src="@Url.Action("GetPDF")"/> 

and other variants without success.

Thanks!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Ok, I finally found the resolution for this. The ShowPDF.razor.cs code behind page is:

public partial class ShowPDF: ComponentBase
{
    private IReportData rdb;       // the database
    private ReportModel aReport;   // report model 

    /*
    aReport.ReportDocument holds the byte[]
    */
    string GetPDF(int ReportId)
    {
        this.rdb = new ReportData();
        aReport = rdb.GetWithReport(ReportId);
        return "data:application/pdf;base64," + Convert.ToBase64String(aReport.ReportDocument);
    }
}

and the ShowPDF.razor page is:

@page "/ShowPDF"
@page "/ShowPDF/{Report:int}"


@code {
    [Parameter]
    public int Report { get; set; }
}
<embed src="@GetPDF(Report)" visible="false" width="1500" height="2000" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...