Home > Mobile >  Blazor - Open PDF File in new Tab
Blazor - Open PDF File in new Tab

Time:01-28

I would like to open a pdf document via a button. Is this possible in Blazor? Thanks for your help

CodePudding user response:

Please try this solution

This is your javascript function

function openFile(data) {
    var link = this.document.createElement('a');
    link.download = data.fileName;
    link.href = data.url;
    link.target ="_blank";
    this.document.body.appendChild(link);
    link.click();
    this.document.body.removeChild(link);
}

This is your razor file, replace url with your pdf download url, clicking on button will open pdf in new browser tab

@inject IJSRuntime JS

<button @onclick=@(()=> JS.InvokeVoidAsync("openFile", new {fileName="anyfileName", url="http://anyurl.com"}))>Download PDF</button>
    

CodePudding user response:

Use HTML?

Download:

<a href=@PdfURL download>Download</a>

or

Open in new tab:

<a href=@PdfURL target="_blank">Open</a>

or

Open in current tab (i.e. Navigate to it) :

<a href=@PdfURL>Open</a>?

  •  Tags:  
  • Related